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>
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>