Hibernate operation Clob type data, hibernateclob type

Source: Internet
Author: User

Hibernate operation Clob type data, hibernateclob type

In POJO, a large String object can be declared as a java. lang. String or java. SQL. Clob type.

When a program loads Clob data from a database, it only loads a logical pointer of Clob data. You must use the Clob. getCaracterStream () method to obtain the data input stream of the Clob type before obtaining the big object data.

See the following code

Package dao; import java. io. bufferedReader; import java. io. IOException; import java. io. reader; import java. math. bigDecimal; import java. SQL. clob; import java. SQL. SQLException; import org. hibernate. lobHelper; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import entity. clobtable; import Factory. hibernateSessionFactory; public class ClobDao {private Session session = n Ull; private Transaction tran = null; public ClobDao () {session = HibernateSessionFactory. getSession ();} public void saveClob (BigDecimal id, String content) {Clobtable ct = new Clobtable (); ct. setId (id); LobHelper lh = session. getLobHelper (); ct. setContent (lh. createClob (content); tran = session. beginTransaction (); try {session. save (ct); tran. commit (); System. out. println ("inserted successfully! ");} Catch (Exception e) {tran. rollback (); System. out. println ("insertion failed") ;}} public void getClob (BigDecimal id) {String hql = "from Clobtable where id =? "; Query query = session. createQuery (hql); query. setBigDecimal (0, id); Clobtable ct = (Clobtable) query. uniqueResult (); Clob clob = ct. getContent (); try {Reader reader = clob. getCharacterStream (); BufferedReader br = new BufferedReader (reader); String content = br. readLine (); System. out. println (content);} catch (SQLException e) {e. printStackTrace (); System. out. println ("read failed! ");} Catch (IOException e) {System. out. println (" read failed! ");}}}


 


Clob operations in hibernate

1. Set the xml field ing link field type to blob
<Property type = "blob">
<Column/>
</Property>
2. Set the corresponding field in the object BEAN to java. SQL. Blob.
Private Blob picture;
Public Blob getPicture (){
Return this. picture;
}
Public void setPicture (Blob picture ){
This. picture = picture;
}
You can check it on my BLOG. the operation method is described as Hibernate2.0.
 
Clob operations in hibernate

It is recommended that the object class type be the same as that in the database... In addition, I think it is of little significance. I hope to consider it.
 

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.