Hibernate map Clob blob field

Source: Internet
Author: User

In many cases, storing large strings or binary data such as pictures hibernate also provides support for BLOBs and CLOB types

BLOB storage is suitable for storing binary data, such as image file Clob using multibyte storage suitable for saving large text, etc.

In MSSQL, image and ntext respectively correspond to blobs and CLOB.

Table mapping file for two types of writing

The mapping type for BLOB format is type= "Java.sql.Clob";

No difference in how the bean is written. Generate getter and setter directly

Save the binary content of a picture

User user=new User();
FileInputStream image=new FileInputStream("image.jpg");
Blob img=Hibernate.createBlob(image);
user.setImage(img);
Clob desc=Hibernate.createClob("abcd");
user.setDesc(desc);
save(user);

Save blob and CLOB type complete

Read:

Reading BLOB type binary data

Blog Img=user.getimage ();

InputStream Inputs=img.getbinarystream ();//Get binary stream

Write to File

FileOutStream fos=new FileOutStream("tempimg.jpg");
byte [] buffer=new byte[102400];
int len;
while((len=inputs.read(buffer))!=-1)
{
fos.write(buffer,0,len);
}
fos.close;
inputs.close;

Oracle execution will receive the following error

streams type cannot be userd in batching

Orcale JDBC does not allow streaming operations to be done in batches (Oracle BLOBs use streaming as data read and write)

user.setImage(Hibernate.createBlob(new byte[1]));
user.setDesc(Hibernate.careteClob(" "));
session.save(user);
session.flush();
session.refresh(user,LockMode.UPGRADE);

To the inside blob to write the actual content ditto

Write to Clob

oracle.sql.Clob clob=user.getDesc();
Writer writer=clob.getCharacterOutputStream();
writer.write("abcd");
session.save(user);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.