• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image insertion into database through JPA

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?




 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Gvn Karthik
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions regarding the exception? How can I do image insertion into database using JPA?
 
Gvn Karthik
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - LOB is short hand for CLOB and BLOB.
 
Gvn Karthik
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how can I solve my issue of inserting bigger images? I mean, more than 4kB?
 
reply
    Bookmark Topic Watch Topic
  • New Topic