File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Image insertion into database through JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Image insertion into database through JPA" Watch "Image insertion into database through JPA" New topic
Author

Image insertion into database through JPA

Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
In the code below I'm reading an image file. The function readImageOldWay(file) reads the file into a byte array as shown by the class ImageService below the class ImageMain. I'm using BLOB datatype in the database to store the image but I'm getting an exception for images larger than 5kB approximately. Check the following code and the exception.




When I insert a bigger image say of size above 150kB, I get the following exception. What should I be doing to get rid of this exception? How can I insert bigger images?




Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Oracle's BLOB data type supports up to 4 GB so should work OK with files of the size you say they are. Are you sure of the size and data type?

What version of the driver are you using? And what version of Oracle?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
What version of Oracle are you using? Oracle thin JDBC driver had a 4k limitation for blob binding, I believe they have fixed this in their latest version. I think OCI never had this limitation.

TopLink supported using a locator to write blob > 4k if you used the Oracle9Platform and define the mapping type as blob.
Unfortunately TopLink Essentials did not include this support, but EclipseLink does.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
I'm using Oracle 10g at present but with the above code I'm not able to write images of higher sizes to database. Is there another way of doing this? Another way which I was thinking of was importing java.sql.Blob and defining a Blob attribute in my entity class. But how do I read it? If I read it as a byte stream using FileStream and InputStream classes and store it in a byte array, how do I set it in the Blob attribute?
Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
Any suggestions regarding the exception? How can I do image insertion into database using JPA?
Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
I have Oracle 10g Express Edition installed. When I checked the size of the image column which is of BLOB type, the size turned out to be 4000. That explains why I was not able to insert an image of higher size.

How do I increase the size of the BLOB field so that I can store bigger images?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


When I checked the size of the image column which is of BLOB type, the size turned out to be 4000.

That statement makes no sense without a unit of measure. 4000 of what?

Reading the Oracle Express documentation, it appears LOB fields support:

A LOB can hold up to a maximum size ranging from 8 terabytes to 128 terabytes depending on how your database is configured.

Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
That size refers to 4000 bytes. Thats why I was able to insert only images of size approximately 4kB. When I created the table in the database, I used the following query:

create table images(id number(5),photo BLOB);

How should I change query so that I can accomodate bigger size images?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Looking at the Oracle Express documentation again, you don't need to change your DDL because the BLOB data type's limits are far higher than 4KB (as my quote from the docs states).

Oracle's LOB data types use semantics to decide how to store the data after the first 4kb, but they store more than 4KB.
Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
So, you mean to say that I can use the datatype BLOB in the back end and I should use the datatype Lob(@Lob) in my Entity class???

Then how do I read the image? As a stream of bytes?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No - LOB is short hand for CLOB and BLOB.
Gvn Karthik
Greenhorn

Joined: Feb 07, 2011
Posts: 22
So how can I solve my issue of inserting bigger images? I mean, more than 4kB?
 
wood burning stoves
 
subject: Image insertion into database through JPA