Use Hibernate to process Blob fields in Oracle

Source: Internet
Author: User

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

  • 1
  • 2
  • Next Page

Related Article

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.