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.