最近常碰到有人问如何运用Hibernate操作Oracle中的Clob、Blob字段,相关问题大概如下几种:
如何读、写数据库中的Clob、Blob字段?
注释方式或者映射文件两种方式是如何实现的?
Clob字段是否可当成一般String来操作?
为什么Clob字段插入正常,但是读取时却为null
JDBC的驱动是不是需要最新的,才能支持Clob当成String?
JDBC驱动不更新,有没有办法实现Clob当成String操作?
。。。。。等等这些问题
下面我将以不同的实现方式的分别来演示,上述提到的问题答案将在具体的实例中给出详细的答案。本文目录:
[一]、Clob字段 - 注解方式的实现
[二]、Clob字段 - 映射文件的实现
[三]、Blob字段 - 注解方式的实现
[四]、Blob字段 - 映射文件的实现
测试的相关源代码 :bigstring_oracle_src.7z
[一]、Clob字段 - 注解 方式的实现
域对象:TbBigStrClob.java
Java代码
package michael.hibernate.bigstring.oracle;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_BIG_STR_CLOB")
public class TbBigStrClob {
private Integer id;
private String name;
private String content;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
/**
* @return the name
*/
@Column(name = "NAME", length = 20)
public String getName() {
return name;
}
/**
* @return the content
*/
@Lob
//@Type(type = "text")
@Column(name = "CONTENT", columnDefinition = "CLOB")
public String getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(String pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TbBigStrClob :: id = [" + id + "],name = [" + name
+ "],content = [" + content + "].";
}
}
Hibernate 配置文件:hibernate.cfg.xml
Xml代码
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.8.49:1521:ora9i
</property>
<property name="connection.username">bm2</property>
<property name="connection.password">bm2</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.SetBigStringTryClob">true</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<!-- 注释 -->
<mapping
class="michael.hibernate.bigstring.oracle.TbBigStrClob" />
</session-factory>
</hibernate-configuration>
测试程序:
Java代码
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestTbBigStr {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 测试Hibernate 注释方法 操作 大字段 Clob
TestTbBigStr.testClob();
}
/**
* 测试Hibernate 注释方法 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testClob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from TbBigStrClob t ";
System.out.println("start test clob ...");
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<TbBigStrClob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init clob data");
List<TbBigStrClob> initlist = initClobData(5);
for (TbBigStrClob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("query data list size = " + list.size());
for (TbBigStrClob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<TbBigStrClob> initClobData(int dataSize) {
List<TbBigStrClob> list = new ArrayList<TbBigStrClob>();
for (int i = 1; i <= dataSize; i++) {
TbBigStrClob vo = new TbBigStrClob();
vo.setName("test_" + i);
vo.setContent("我是大字段<Clob>,at date:" + new Date());
list.add(vo);
}
return list;
}
}
运行结果:
../../hibernate.cfg.xml configure
start test clob ...
Hibernate: select tbbigstrcl0_.ID as ID2_, tbbigstrcl0_.NAME as NAME2_, tbbigstrcl0_.CONTENT as CONTENT2_ from MY_TB_BIG_STR_CLOB tbbigstrcl0_
start to init clob data
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into MY_TB_BIG_STR_CLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_CLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_CLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_CLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_CLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: select tbbigstrcl0_.ID as ID2_, tbbigstrcl0_.NAME as NAME2_, tbbigstrcl0_.CONTENT as CONTENT2_ from MY_TB_BIG_STR_CLOB tbbigstrcl0_
query data list size = 5
TbBigStrClob :: id = [1813873],name = [test_1],content = [我是大字段<Clob>,at date:Mon Aug 22 13:10:38 CST 2011].
TbBigStrClob :: id = [1813874],name = [test_2],content = [我是大字段<Clob>,at date:Mon Aug 22 13:10:38 CST 2011].
TbBigStrClob :: id = [1813875],name = [test_3],content = [我是大字段<Clob>,at date:Mon Aug 22 13:10:38 CST 2011].
TbBigStrClob :: id = [1813876],name = [test_4],content = [我是大字段<Clob>,at date:Mon Aug 22 13:10:38 CST 2011].
TbBigStrClob :: id = [1813877],name = [test_5],content = [我是大字段<Clob>,at date:Mon Aug 22 13:10:38 CST 2011].
运行结果可见:注解方式下,插入Clob数据和读取Clob数据均正常。
Tips:
1. 在Oracle10g + 10g驱动(或者Oracle11g + 11g驱动)的环境下,只需要在Clob字段上,增加注解@Column(name = "CONTENT", columnDefinition = "CLOB"),即可成功实现建表、Clob字段的读写操作。
2. 在Oracle9i+9i的驱动环境下,仅增加注释@Column(name = "CONTENT", columnDefinition = "CLOB")时,建表、写人数据均能正常,但读取时会显示数据为空。如果在Oracle9i下又必须用9i的驱动,可以在Clob字段中再增加一行注解@Lob 或者 @Type(type = "text")。(运用spring中org.springframework.jdbc.support.lob.OracleLobHandler,这个没有具体实践过,就不在本文展开了 )
3. 在Oracle9i+10g的驱动环境下,增加注解@Column(name = "CONTENT", columnDefinition = "CLOB"),同样可以成功实现建表、Clob字段的读写操作,可见9i的驱动对Clob支持不是很好,但10g、11g已经做了更新。
[二]、Clob字段 - 映射文件的实现
bean文件:BigStrClob.java
Java代码
package michael.hibernate.bigstring.oracle;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class BigStrClob {
private Integer id;
private String name;
private String content;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(String pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "BigStrClob :: id = [" + id + "],name = [" + name
+ "],content = [" + content + "].";
}
}
映射文件:tb_bigstr.hb.xml
Java代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.hibernate.bigstring.oracle">
<class name="BigStrClob" table="MY_HB_BIG_STR_CLOB">
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string"
not-null="false" length="20" />
<property name="content" column="CONTENT" type="text"
not-null="false" />
</class>
</hibernate-mapping>
在hibernate.cfg.xml文件中增加域对象的映射文件:
Xml代码
<!-- 映射 -->
<mapping
resource="michael/hibernate/bigstring/oracle/tb_bigstr.hb.xml" />
测试程序:
Java代码
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestHbBigStr {
/**
* @param args
*/
public static void main(String[] args) {
// 测试Hibernate 映射文件 操作 大字段 Clob
TestHbBigStr.testClob();
}
/**
* 测试Hibernate 映射文件 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testClob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from BigStrClob t ";
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<BigStrClob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init clob data");
List<BigStrClob> initlist = initClobData(5);
for (BigStrClob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("Query data list size = " + list.size());
for (BigStrClob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<BigStrClob> initClobData(int dataSize) {
List<BigStrClob> list = new ArrayList<BigStrClob>();
for (int i = 1; i <= dataSize; i++) {
BigStrClob vo = new BigStrClob();
vo.setName("test_" + i);
vo.setContent("我是大字段<Clob>,at date:" + new Date());
list.add(vo);
}
return list;
}
}
运行结果如下:
../../hibernate.cfg.xml configure
Hibernate: select bigstrblob0_.id as id1_, bigstrblob0_.NAME as NAME1_, bigstrblob0_.CONTENT as CONTENT1_ from MY_HB_BIG_STR_BLOB bigstrblob0_
start to init blob data
Hibernate: select max(id) from MY_HB_BIG_STR_BLOB
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: select bigstrblob0_.id as id1_, bigstrblob0_.NAME as NAME1_, bigstrblob0_.CONTENT as CONTENT1_ from MY_HB_BIG_STR_BLOB bigstrblob0_
Query data list size = 5
BigStrBlob :: id = [1],name = [test_1],content = [我是大字段<Blob>,at date:Mon Aug 22 13:28:51 CST 2011].
BigStrBlob :: id = [2],name = [test_2],content = [我是大字段<Blob>,at date:Mon Aug 22 13:28:51 CST 2011].
BigStrBlob :: id = [3],name = [test_3],content = [我是大字段<Blob>,at date:Mon Aug 22 13:28:51 CST 2011].
BigStrBlob :: id = [4],name = [test_4],content = [我是大字段<Blob>,at date:Mon Aug 22 13:28:51 CST 2011].
BigStrBlob :: id = [5],name = [test_5],content = [我是大字段<Blob>,at date:Mon Aug 22 13:28:51 CST 2011].
从运行结果可见:映射文件的方式下,插入Clob数据和读取Clob数据均正常
Tips: 如果是映射文件的,只需要把Clob字段的类型指定为text即:type="text"
[三]、Blob字段 - 注解 方式的实现
域对象的文件:TbBigStrBlob.java
Java代码
package michael.hibernate.bigstring.oracle;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_BIG_STR_BLOB")
public class TbBigStrBlob {
private Integer id;
private String name;
private byte[] content;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
/**
* @return the name
*/
@Column(name = "NAME", length = 20)
public String getName() {
return name;
}
/**
* @return the content
*/
@Lob
@Column(name = "CONTENT", columnDefinition = "BLOB")
public byte[] getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(byte[] pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TbBigStrBlob :: id = [" + id + "],name = [" + name
+ "],content = [" + new String(content) + "].";
}
}
在hibernate.cfg.xml中增加域对象:
Xml代码
<mapping class="michael.hibernate.bigstring.oracle.TbBigStrBlob" />
运行测试程序:
Java代码
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestTbBigStr {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 测试Hibernate 注释方法 操作 大字段 Blob
TestTbBigStr.testBlob();
}
/**
* 测试Hibernate 注释方法 操作 大字段 Blob
*/
@SuppressWarnings("unchecked")
public static void testBlob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from TbBigStrBlob t ";
System.out.println("start test clob ...");
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<TbBigStrBlob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init blob data");
List<TbBigStrBlob> initlist = initBlobData(5);
for (TbBigStrBlob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("query data list size = " + list.size());
for (TbBigStrBlob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<TbBigStrBlob> initBlobData(int dataSize) {
List<TbBigStrBlob> list = new ArrayList<TbBigStrBlob>();
for (int i = 1; i <= dataSize; i++) {
TbBigStrBlob vo = new TbBigStrBlob();
vo.setName("test_" + i);
vo.setContent(("我是大字段<Blob>,at date:" + new Date()).getBytes());
list.add(vo);
}
return list;
}
}
运行结果如下:
../../hibernate.cfg.xml configure
start test clob ...
Hibernate: select tbbigstrbl0_.ID as ID3_, tbbigstrbl0_.NAME as NAME3_, tbbigstrbl0_.CONTENT as CONTENT3_ from MY_TB_BIG_STR_BLOB tbbigstrbl0_
start to init blob data
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into MY_TB_BIG_STR_BLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_BLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_BLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_BLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: insert into MY_TB_BIG_STR_BLOB (NAME, CONTENT, ID) values (?, ?, ?)
Hibernate: select tbbigstrbl0_.ID as ID3_, tbbigstrbl0_.NAME as NAME3_, tbbigstrbl0_.CONTENT as CONTENT3_ from MY_TB_BIG_STR_BLOB tbbigstrbl0_
query data list size = 5
TbBigStrBlob :: id = [1813878],name = [test_1],content = [我是大字段<Blob>,at date:Mon Aug 22 13:40:56 CST 2011].
TbBigStrBlob :: id = [1813879],name = [test_2],content = [我是大字段<Blob>,at date:Mon Aug 22 13:40:56 CST 2011].
TbBigStrBlob :: id = [1813880],name = [test_3],content = [我是大字段<Blob>,at date:Mon Aug 22 13:40:56 CST 2011].
TbBigStrBlob :: id = [1813881],name = [test_4],content = [我是大字段<Blob>,at date:Mon Aug 22 13:40:56 CST 2011].
TbBigStrBlob :: id = [1813882],name = [test_5],content = [我是大字段<Blob>,at date:Mon Aug 22 13:40:56 CST 2011].
从运行结果可见:注解的方式下,插入Blob数据和读取Blob数据均正常
Tips: 对于byte[] 类型的字段如果要映射成Blob字段,只需要在该字段增加如下注解即可:
Java代码
@Lob
@Column(name = "CONTENT", columnDefinition = "BLOB")
[四]、Blob字段 - 映射文件的实现
映射文件对象:BigStrBlob.java
Java代码
package michael.hibernate.bigstring.oracle;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class BigStrBlob {
private Integer id;
private String name;
private byte[] content;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the content
*/
public byte[] getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(byte[] pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "BigStrBlob :: id = [" + id + "],name = [" + name
+ "],content = [" + new String(content) + "].";
}
}
映射文件:tb_bigstr.hb.xml
Xml代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.hibernate.bigstring.oracle">
<class name="BigStrBlob" table="MY_HB_BIG_STR_BLOB">
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string"
not-null="false" length="20" />
<property name="content" column="CONTENT" type="binary"
not-null="false" />
</class>
</hibernate-mapping>
测试程序:
Java代码
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestHbBigStr {
/**
* @param args
*/
public static void main(String[] args) {
// 测试Hibernate 映射文件 操作 大字段 Blob
TestHbBigStr.testBlob();
}
/**
* 测试Hibernate 映射文件 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testBlob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from BigStrBlob t ";
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<BigStrBlob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init blob data");
List<BigStrBlob> initlist = initBlobData(5);
for (BigStrBlob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("Query data list size = " + list.size());
for (BigStrBlob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<BigStrBlob> initBlobData(int dataSize) {
List<BigStrBlob> list = new ArrayList<BigStrBlob>();
for (int i = 1; i <= dataSize; i++) {
BigStrBlob vo = new BigStrBlob();
vo.setName("test_" + i);
vo.setContent(("我是大字段<Blob>,at date:" + new Date()).getBytes());
list.add(vo);
}
return list;
}
}
运行结果如下:
../../hibernate.cfg.xml configure
Hibernate: select bigstrblob0_.id as id1_, bigstrblob0_.NAME as NAME1_, bigstrblob0_.CONTENT as CONTENT1_ from MY_HB_BIG_STR_BLOB bigstrblob0_
start to init blob data
Hibernate: select max(id) from MY_HB_BIG_STR_BLOB
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: insert into MY_HB_BIG_STR_BLOB (NAME, CONTENT, id) values (?, ?, ?)
Hibernate: select bigstrblob0_.id as id1_, bigstrblob0_.NAME as NAME1_, bigstrblob0_.CONTENT as CONTENT1_ from MY_HB_BIG_STR_BLOB bigstrblob0_
Query data list size = 5
BigStrBlob :: id = [1],name = [test_1],content = [我是大字段<Blob>,at date:Mon Aug 22 13:47:50 CST 2011].
BigStrBlob :: id = [2],name = [test_2],content = [我是大字段<Blob>,at date:Mon Aug 22 13:47:50 CST 2011].
BigStrBlob :: id = [3],name = [test_3],content = [我是大字段<Blob>,at date:Mon Aug 22 13:47:50 CST 2011].
BigStrBlob :: id = [4],name = [test_4],content = [我是大字段<Blob>,at date:Mon Aug 22 13:47:50 CST 2011].
BigStrBlob :: id = [5],name = [test_5],content = [我是大字段<Blob>,at date:Mon Aug 22 13:47:50 CST 2011].
从运行结果可见:映射文件的方式下,插入Blob数据和读取Blob数据均正常
Tips: 对于是映射文件的方式,Blob字段的类型指定为binary即:type=" binary "
转载来自:Michael's blog @ http://sjsky.iteye.com