随笔-204  评论-90  文章-8  trackbacks-0
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
posted on 2006-05-11 18:40 一凡 阅读(2148) 评论(0)  编辑  收藏 所属分类: JAVA FRAMEWORK

只有注册用户登录后才能发表评论。


网站导航: