Hibernate操作Clob類型資料是怎樣弄的,hibernateclob

來源:互聯網
上載者:User

Hibernate操作Clob類型資料是怎樣弄的,hibernateclob

在POJO中字串大對象可以聲明成一個java.lang.String或java.sql.Clob類型。

當程式從資料庫中載入Clob類型資料時,僅僅載入了一個Clob類型的資料的邏輯指標。我們需要通過使用Clob.getCaracterStream()方法得到Clob類型的資料輸入流之後才能擷取大對象資料。

看下面具體代碼

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 = null;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("插入成功!");}catch(Exception e){tran.rollback();System.out.println("插入失敗");}}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("讀取失敗!");} catch (IOException e) {System.out.println("讀取失敗!");}}}

hibernate操作clob問題

1. 設定xml欄位對應關係欄位類型為blob
<property type="blob">
<column />
</property>
2. 設定實體BEAN中對應欄位為java.sql.Blob
private Blob picture;
public Blob getPicture() {
return this.picture;
}
public void setPicture(Blob picture) {
this.picture = picture;
}
你上我BLOG上看一下,上面有操作方式,也是Hibernate2.0
 
hibernate操作clob問題

最好實體類的類型和資料庫裡面的是一樣的。。。而且的話,像這樣的 我覺得意義不大。希望考慮下。
 

相關文章

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.