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);