1. Bolb field description:
Writing Blob fields is very different from writing other types of fields. Because Blob itself has a cursor, you must use cursor to operate blob. Therefore, before writing Blob, you must obtain the cursor to write data. How can you obtain the Blob cursor?
This requires you to insert an empty blob first, which will create a blob cursor, and then you can query the cursor of the empty blob using select. In this way, you can perform two steps, you can get the blob cursor to write blob data.
2. Save the Bolb type field:
The configuration file of Hibernate will not be written. You need to set the Blob field type to java. SQL. Blob. The following code is directly used.
Public void save (ZyglBlxx bean, InputStream ins) throws WebServiceException {
// TODO Auto-generated method stub
Session session = this. getHibernateTemplate (). getSessionFactory (). openSession ();
// Ins.
// Out. write (B)
Try {
Bean. setDoccontent (BLOB. getEmptyBLOB ());
Transaction tr = session. beginTransaction ();
Bean. setVId (WebServiceEditUtils. getPk ());
Session. save (bean );
Session. flush ();
Session. refresh (bean, LockMode. UPGRADE );
If (ins! = Null ){
SerializableBlob sb = (SerializableBlob) bean. getDoccontent ();
BLOB B = (BLOB) sb. getWrappedBlob ();
OutputStream out = B. getBinaryOutputStream ();
Int len =-1;
Byte [] bt = new byte [2048]; // it can be adjusted according to the actual situation. We recommend that you use 1024, that is, 1 kb for each read.
While (len = (ins. read (bt )))! =-1 ){
Out. write (bt, 0, len); // do not use OS. write (bt) directly)
}
Out. flush ();
Ins. close ();
Out. close ();
}
Session. flush ();
Tr. commit ();
Session. close ();
} Catch (IOException e ){
E. printStackTrace ();
Throw ExceptionManager. getExcption ("18 ");
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Throw ExceptionManager. getExcption ("19 ");
}
}
In order to save the file more conveniently, the parameters here are directly received as InputStream. Other types are similar
Hibernate details: click here
Hibernate: click here
Hibernate Chinese manual PDF