用Hibernate操作Oracle Blob

1.在HBM里对应CLOB型的字段为:java.sql.Clob,用MyEclipse DataExplorer生成后手动改一下即可,当然POJO也一样啊,如:
HBM:
<
hibernate-mapping> 
<class name="com.chinacars.filterdata.beans.TblWapNews2" table="TBL_WAP_NEWS2">
        
<id name="id" type="java.lang.Long">
            
<column name="ID" precision="22" scale="0" />
        
</id>
      
<property name="content" type="java.sql.Clob">
            
<column name="CONTENT" />
        
</property>

</class>
</hibernate-mapping>
POJO:
import  java.sql.Clob;
public   class  test  {
   
private Clob content;
   
}

2.首先要给Clob里写一个值,如:注意看一下有底色的地方就明白了吧
package  com.chinacars.filterdata.modidata;

import  java.lang.reflect.InvocationTargetException;
import  java.sql.Clob;
import  java.sql.SQLException;
import  java.util.List;

import  org.apache.commons.beanutils.PropertyUtils;
import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.hibernate.Hibernate;

import  com.chinacars.filterdata.beans.TblWapNews1;
import  com.chinacars.filterdata.beans.TblWapNews2;
import  com.chinacars.filterdata.dao.WapNews1DAO;
import  com.chinacars.filterdata.dao.WapNews2DAO;
import  com.chinacars.filterdata.util.StringProcess;

public   class  ModiData2  {
    
private static final Log logger = LogFactory.getLog(ModiData2.class);

    
public static void main(String[] args) {

        WapNews1DAO wapNews1DAO 
= new WapNews1DAO();
        WapNews2DAO wapNews2DAO 
= new WapNews2DAO();

        TblWapNews1 news1 
= new TblWapNews1();
        TblWapNews2 news2 
= new TblWapNews2();

        List list 
= wapNews1DAO.getWapNews();
        Clob contentClob 
= null;
        String cStr 
= "1";
        String title;
        
        
for (int i = 0; i < list.size(); i++{
            logger.info(
"============The number : " + i);
            news1 
= (TblWapNews1) list.get(i);
            contentClob 
= news1.getContent();
            
try {
                PropertyUtils.copyProperties(news2, news1);
            }
 catch (IllegalAccessException e) {
                logger.error(e);
                e.printStackTrace();
            }
 catch (InvocationTargetException e) {
                logger.error(e);
                e.printStackTrace();
            }
 catch (NoSuchMethodException e) {
                logger.error(e);
                e.printStackTrace();
            }

            title 
= news1.getTitle();
            
if(title != null{
                title 
= title.replaceAll("""");
                news2.setTitle(title);
            }

            news2.setContent(Hibernate.createClob(cStr));
            
try {
                wapNews2DAO.saveWapNews(news2, StringProcess
                        .delInvalidChar(contentClob.getSubString(
1,
                                (
int) contentClob.length())));
                
            }
 catch (SQLException e) {
                logger.error(e);
                e.printStackTrace();
            }

        }


    }


}

3.开始写入这内容,最主要的是这里,我在这里卡了好久……
   主是下面红色的部分,其它的有色部分都能搜到:看下面的代码吧:^_^
package  com.chinacars.filterdata.dao;

import  java.io.Writer;
import  java.sql.Clob;

import  oracle.sql.CLOB;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.hibernate.LockMode;
import  org.hibernate.Session;
import  org.hibernate.Transaction;
import  org.hibernate.lob.SerializableClob;

import  com.chinacars.filterdata.beans.TblWapNews2;

public   class  WapNews2DAO  {
    
    
private static final Log logger = LogFactory.getLog(WapNews2DAO.class);

    
private Session oraSession;

    
private Transaction tx;

    
public boolean saveWapNews(TblWapNews2 news, String content) {
        
try {
            oraSession 
= SessionFactory.currentORASession();
            tx 
= oraSession.beginTransaction();
            oraSession.save(news);
            oraSession.flush();
            oraSession.refresh(news, LockMode.UPGRADE);
            
            
SerializableClob clob 
= (SerializableClob) news.getContent();
            Clob jClob 
= clob.getWrappedClob();
            CLOB oClob 
=
 (CLOB)jClob;
            Writer wt 
=
 oClob.getCharacterOutputStream();            
            
//byte[] bData = content.getBytes();

            wt.write(content);
            wt.close();
            oraSession.flush();
            tx.commit();
            logger.info(
"===================  INSERT OK ");
        }
 catch (Exception e) {
            
if (tx != null)
                tx.rollback();
            logger.info(
"===================  NOT INSERT ");
            logger.info(e);
            e.printStackTrace();
        }
 finally {
            oraSession.close();
        }

        
return true;
    }


}


哈哈,就写到这里了,快没车了……
有不懂的朋友,发e-mail吧!
erdosqiao@163.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值