最近常碰到有人问如何运用Hibernate操作Oracle中的Clob、Blob字段,相关问题大概如下几种:
- 如何读、写数据库中的Clob、Blob字段?
- 注释方式或者映射文件两种方式是如何实现的?
- Clob字段是否可当成一般String来操作?
- 为什么Clob字段插入正常,但是读取时却为null
- JDBC的驱动是不是需要最新的,才能支持Clob当成String?
- JDBC驱动不更新,有没有办法实现Clob当成String操作?
- 。。。。。等等这些问题
下面我将以不同的实现方式的分别来演示,上述提到的问题答案将在具体的实例中给出详细的答案。本文目录:
- [一]、Clob字段 – 注解方式的实现
- [二]、Clob字段 – 映射文件的实现
- [三]、Blob字段 – 注解方式的实现
- [四]、Blob字段 – 映射文件的实现
测试的相关源代码 :bigstring_oracle_src.zip
[一]、Clob字段 – 注解 方式的实现
域对象:TbBigStrClob.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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://www.micmiu.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!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">mytest</property> <property name="connection.password">123456</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> |
测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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://www.micmiu.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”),某些驱动下好像可以正常,但是有些版本9i的驱动下如果clob字段的数据量很大 还是会有异常发生,一般在项目中借助spring的OracleLobHandler完成兼容。
运用spring中org.springframework.jdbc.support.lob.OracleLobHandler 实现对clob的操作的基本步骤:
增加配置信息如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!-- spring clob handler --> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" lazy-init="true" /> <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> <property name="nativeJdbcExtractor"> <ref bean="nativeJdbcExtractor" /> </property> </bean> |
在sessionFactory的配置中增加属性 lobHandler 的配置:
1 2 3 4 5 6 7 |
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="lobHandler" ref="oracleLobHandler" /> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> |
修改domain对象中clob 对应属性的getXxx()方法上的注解:
1 2 3 4 5 6 |
@Lob @Type(type = "org.springframework.orm.hibernate3.support.ClobStringType") @Column(name = "CONTENT", columnDefinition = "CLOB") public String getContent() { return content; } |
程序中对该字段的操作和String类型的一样,无需额外操作。
3. 在Oracle9i+10g的驱动环境下,增加注解@Column(name = “CONTENT”, columnDefinition = “CLOB”),同样可以成功实现建表、Clob字段的读写操作,可见9i的驱动对Clob支持不是很好,但10g、11g已经做了更新。
[二]、Clob字段 – 映射文件的实现
bean文件:BigStrClob.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package michael.hibernate.bigstring.oracle; /** * * @blog http://www.micmiu.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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文件中增加域对象的映射文件:
1 2 |
<!-- 映射 --> <mapping resource="michael/hibernate/bigstring/oracle/tb_bigstr.hb.xml" /> |
测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
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://www.micmiu.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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://www.micmiu.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中增加域对象:
1 |
<mapping class="michael.hibernate.bigstring.oracle.TbBigStrBlob" /> |
运行测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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://www.micmiu.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字段,只需要在该字段增加如下注解即可:
1 2 |
@Lob @Column(name = "CONTENT", columnDefinition = "BLOB") |
[四]、Blob字段 – 映射文件的实现
映射文件对象:BigStrBlob.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package michael.hibernate.bigstring.oracle; /** * * @blog http://www.micmiu.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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> |
测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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://www.micmiu.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 “
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/hibernate/hibernate-oarcle-clob-blob/
0 条评论。