Hibernate中处理Oracle中大数据Clob类型

    今日开发后台音档系统,需要把歌词内容插入到一个大数据类型中CLOB中去。运用传统的插入与更新不能实现,于是在网上搜索整理。主要说下使用方法
     实体类:
   public class MtopProduct implements Serializable {

private Clob lyricclob;

private String words;
private String melod;

//是Clob数据类型的附加一个字段,数据库中无此字段,也无需在配置文件中配置,主要是在DAO方法中运用来操作Clob类型
private String lyricClobString;
        //省略了get和set方法
}
 
    根据网上的内容,自己写了一个DAO类,在SAVE和UPDATE方法中专门类处理Clob类型。

  public class MtopProductDao extends DAO {
   private static Log log =LogFactory.getLog(MtopProductDao.class);
  
@Override
protected Class getBeanClass() {
return MtopProduct.class;
}

public MtopProductDao(){

}
   
/**
* 通过ID得到对象
* @param id
* @return
*/
public MtopProduct load(int id){
return (MtopProduct)super.load(id);
}

public boolean saveObject(Object obj)
{
//更新前首先要清空原clob字段的内容
        if (obj == null)
        {
        return false;
        }
       
        MtopProduct productBean = (MtopProduct )obj;
       
        try
        {
beginTransaction();

        productBean.setLyricClob(oracle.sql.CLOB.empty_lob());
        getSession().save(productBean);
getSession().flush();
getSession().refresh(productBean, org.hibernate.LockMode.UPGRADE);

SerializableClob sc = (SerializableClob)productBean.getLyricClob();
java.sql.Clob wrapclob = sc.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)wrapclob;
clob.putString(1, productBean.getLyricClobString());

getSession().save(productBean);

commit();
return true;
}
        catch (Exception er)
        {
er.printStackTrace();
if (log.isErrorEnabled()) {
log.error("对‘对像’进行持久化时出错..." + getBeanClass().getName());
log.error("错误信息提示" + er.getMessage());
}
rollback();
}
        return false;
}
public boolean updateObject(Object obj)
{
if (obj == null)
        {
        return false;
        }

MtopProduct productBean = (MtopProduct)obj;
       
        //初始化LyricClob字段
        productBean.setLyricclob(org.hibernate.Hibernate.createClob(" "));
       
        try
        {
beginTransaction();

        productBean.setLyricclob(oracle.sql.CLOB.empty_lob());
        getSession().save(productBean);
getSession().flush();
getSession().refresh(productBean, org.hibernate.LockMode.UPGRADE);

SerializableClob sc = (SerializableClob)productBean.getLyricclob();
java.sql.Clob wrapclob = sc.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)wrapclob;
clob.putString(1, productBean.getLyricClobString());

getSession().save(productBean);
           
commit();
return true;
}
        catch (Exception er)
        {
er.printStackTrace();
if (log.isErrorEnabled()) {
log.error("对‘对像’进行持久化时出错..." + getBeanClass().getName());
log.error("错误信息提示" + er.getMessage());
}
rollback();
}
        return false;
}

hbm.xml配置文件:
       <property name="lyricclob" type="java.sql.Clob">
             <column name="LYRICCLOB"/>
           </property>
           <property name="words" type="java.lang.String">
              <column name="WORDS" length="20"/>
           </property>
          <property name="melod" type="java.lang.String">
              <column name="MELOD" length="20"/>
           </property>

方法调用:
           mp =mpdao.load(sid);

         mp.setWords(words);

         mp.setMelod(melod);

mp.setLyricClobString(lrccontent);  //直接插入值到此附加值中
strFlag= mpdao.updateObject(mp);

此些代码主要是自己开发所用,经验尚浅,一定存在很多漏洞,错误之处,希望大家多提意见,多多指教,谢谢!

你可能感兴趣的:(DAO,oracle,sql,xml,Hibernate)