oracle8+hibernate

17 篇文章 0 订阅
2 篇文章 0 订阅
Clob字段在不同版本的oracle和hibernate一起使用时,其保存方法是不一样的,以前我接触过oracle9+hibernate2.1并且把成功的方法也传上来了,现在我又遇到oracle8+hibernate,于是我又做了测试,以下是测试成功的代码:
package oracle.clob;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import oracle.clob.vo.Foo;

import org.apache.log4j.Logger;

/**
 * @author yanlin
 */
public class ClobTest{   
   
    public static void main(String[] args){
       
        Logger log = Logger.getLogger(ClobTest.class);
        String content = "你好";
        SessionFactory sf = null;
        try{
        Configuration conf = new Configuration();
        conf.addClass(Foo.class);
        sf = conf.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();         
        Foo foo = new Foo();               
        foo.setValue( Hibernate.createClob(" ") );
        foo.setId(new Integer(12));
       
        /**  oracle8+hibernate1.2.1:
        s.save(foo);
        tx.commit();
        // a session only can be used once at once
        s.close();
       
        // save clob field the second time
        s = sf.openSession();
        tx = s.beginTransaction();       
        // lock this field
        foo = (Foo) s.load( Foo.class, foo.getId(), LockMode.UPGRADE );         
        oracle.sql.CLOB clob = (oracle.sql.CLOB) foo.getValue();
        java.io.Writer pw = clob.getCharacterOutputStream();
        // content is a String       
        pw.write(content);
        pw.close();
        tx.commit();
        s.close();
        */
       
        /**  oracle8+hibernate2.1
        s = sf.openSession();
        tx = s.beginTransaction();       
        foo.setValue( Hibernate.createClob(" ") );
        s.save(foo);
        s.flush();
//         store twice
        s.refresh(foo, LockMode.UPGRADE);
         // grabs an Oracle CLOB
        oracle.sql.CLOB clob = (oracle.sql.CLOB) foo.getValue();
        java.io.Writer pw =    clob.getCharacterOutputStream();
        pw.write(content);
        pw.close();
        tx.commit();
        s.close();
        */
       
        // Get the clob content that just be inserted into the dataBase
        s = sf.openSession();
        tx = s.beginTransaction();       
        // lock this field
        foo = (Foo) s.load(Foo.class, foo.getId());         
        oracle.sql.CLOB clob1 = (oracle.sql.CLOB) foo.getValue();
        //when you use Clob.getSubString(long,int),the first argument must be end with "L" or "l"
        System.out.println(clob1.getSubString(1L, 4));
        // content is a String           
        tx.commit();
        s.close();
       
       
        }catch(Exception e){       
            e.printStackTrace();
        }       

    }
}

/*******************************************************************************************************************
package oracle.clob.vo;

import java.sql.Clob;

public class Foo {
    private Integer id;
    private Clob value ;
   
    public Foo(){};

    public Clob getValue() {
        return value;
    }

    public void setValue(Clob value) {
        this.value = value;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
   
}

/*******************************************************************************************************************
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="oracle.clob.vo">
    <class name="Foo" table="FOO">
        <id name="id" type="int" column="ID" unsaved-value="null">
            <generator class="assigned"></generator>
        </id>
        <property name="value" column="CONTENT" ></property>
    </class>   
</hibernate-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值