- 浏览: 921603 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (263)
- J2EE (9)
- Spring (11)
- Hibernate (11)
- Struts (5)
- opensource (19)
- Hadoop (28)
- 架构设计 (8)
- 企业应用 (10)
- SNMP (8)
- SSO (4)
- webservice (11)
- RPC (2)
- 编程语言 (0)
- Java (30)
- Javascript (5)
- NoSQL (11)
- 数据库 (0)
- oracle (8)
- MySQL (3)
- web (1)
- Android (2)
- Linux (15)
- 软件工具 (15)
- 项目构建 (11)
- 测试工具 (2)
- Exception (19)
- 杂谈 (4)
- Groovy (5)
- Nodejs (1)
- MacOSX (4)
最新评论
-
fighhin:
decode(BinaryBitmap,java.util.M ...
条形码/二维码之开源利器ZXing图文介绍 -
u013489005:
追问:楼主,请问有中文文档么?我的邮箱是frankgray@s ...
Java表达式计算引擎:Expr4J -
u013489005:
感谢博主 需要引入的包是import java.io.*;im ...
Java表达式计算引擎:Expr4J -
calosteward:
感谢楼主分享。。 Zxing 我听说过的。__________ ...
条形码/二维码之开源利器ZXing图文介绍 -
u013810758:
judasqiqi 写道感谢楼主!想请问楼主一下这个生成的图片 ...
Java实现二维码QRCode的编码和解码
blog迁移至 :http://www.micmiu.com
最近常碰到有人问如何运用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
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
<!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>
测试程序:
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; } }
运行结果:
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
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
<?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文件中增加域对象的映射文件:
<!-- 映射 --> <mapping resource="michael/hibernate/bigstring/oracle/tb_bigstr.hb.xml" />
测试程序:
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: 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
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中增加域对象:
<mapping class="michael.hibernate.bigstring.oracle.TbBigStrBlob" />
运行测试程序:
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; } }
运行结果如下:
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字段,只需要在该字段增加如下注解即可:
@Lob @Column(name = "CONTENT", columnDefinition = "BLOB")
[四]、Blob字段 - 映射文件的实现
映射文件对象:BigStrBlob.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 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>
测试程序:
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: 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 "
本文连接:http://sjsky.iteye.com/blog/1154916
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------
- bigstring_oracle_src.7z (2.6 KB)
- 下载次数: 42
评论
//@Type(type="org.springframework.orm.hibernate3.support.ClobStringType")
//@Type(type="org.hibernate.type.ClobType")
@Column(name = "DATA_INFO", columnDefinition = "CLOB")
太给力了,找了半天用了上面的3种实现均有问题,最后用columnDefinition = "CLOB" 轻松解决了。
可以通过SerializableBlob平滑的从java.sql.Blob过度到oracle.sql.BLOB
SerializableBlob blob = (SerializableBlob) lazy2.getBlobname();
java.sql.Blob wrapBlob = blob.getWrappedBlob();
BLOB tmpBlob = (BLOB) wrapBlob;
//流读写
//重刷缓存 提交
大了就会出问题
这个我有时间测试下的 ,奇怪了
大了就会出问题
发表评论
-
Hibernate实现JPA的示例
2013-09-23 10:50 797目录 概述 演示环境 示例 详见:http:// ... -
Hibernate中one-to-many双向关联QBC查询示例
2013-08-20 17:17 873目录: 应用场景 实例演示 详见:http: ... -
Hibernate中one-to-many双向关联HQL查询示例
2013-08-20 17:16 1493目录: 应用场景 实例演示 详见:http: ... -
Hibernate注解实现one-to-many双向关联的实例演示
2013-08-20 17:14 1715如何注解的方式实现one-to-many 、many- ... -
HQL实现多对多关联查询(无关系表的映射)
2013-06-13 09:56 1356目录: 应用场景 演示 详见:http://www ... -
Hibernate注解实现单表递归树形结构
2013-03-15 08:47 1438目录: 概述 环境 代码示例 测试结果 详见 ... -
Hibernate4中获取SessionFactory的方法
2013-03-13 23:06 1738Hibernate4 中 Configuration ... -
Hibernate+EhCache配置二级缓存
2011-12-15 21:51 2872blog迁移至 :http://www.micmiu. ... -
Hibernate动态建表
2010-12-13 09:53 3331blog迁移至:http://www.micmiu.c ... -
hibernate3 注释生成复合主键或者嵌入式主键的方法及实例
2010-03-08 16:23 2219blog迁移至:http://www ...
相关推荐
在了解如何使用OARCLE 11g RAC + ASM安装手册之前,我们需要先对Oracle的RAC和ASM技术进行一个简要的了解。RAC(Real Application Clusters)是Oracle数据库的一项高可用性技术,它允许多个Oracle实例共享同一个物理...
在DML操作中,我们还可以使用WHERE子句来设置条件,以精确地定位我们要操作的数据。 DDL(Data Definition Language)是用于创建和修改数据库结构的语言,比如定义和修改表、视图、索引等。CREATE TABLE语句用于...
- **性能影响**:相对于归档模式,非归档模式可能在某些情况下提供更好的性能,因为它减少了I/O操作。 #### 三、如何在归档模式与非归档模式之间进行切换 1. **关闭数据库**:首先需要关闭数据库实例,以避免切换...
本文将深入探讨“重装Oracle”过程中必须注意的关键点,包括但不限于卸载步骤、注册表清理、环境变量调整及后续配置等,旨在为IT专业人士提供全面的指导。 ### 一、彻底卸载Oracle #### 1. 停止所有Oracle服务 ...
Oracle是全球广泛使用的大型关系型数据库管理系统之一,其在企业级数据存储和管理中扮演着重要角色。"Oracle培训资料大全"涵盖了Oracle系统的多个关键方面,对于想要学习或提升Oracle技能的人来说,是一份非常宝贵的...
Oracle数据库是全球广泛使用的数据库管理系统,而PL/SQL(Procedural Language/Structured Query Language)则是Oracle数据库系统中的一种过程化编程语言,用于编写数据库应用程序。PL/SQL结合了SQL的强大查询功能和...
在Java编程环境中,将Oracle数据库的表结构导出到Excel是一种常见的需求,特别是在数据库管理和数据分析时。这个场景可以通过两个核心类来实现:`TableStructureToExcel.java`和`ConnectionOracle.java`。这两个类...
标题中提到的“Oracle®Database 2Day+ Security Guide 11g Release 2 (11.2) E10575-08”指的是Oracle数据库的安全指南文档,适用于Oracle数据库版本11g的第二个更新版(11.2)。E10575-08是该文档的版本号,而日期...
这样用户SONIC的所有对象被输出到文件中。 3、表模式: EXP SONIC/SONIC BUFFER=64000 FILE=C:\SONIC.DMP OWNER=SONIC TABLES=(SONIC) 这样用户SONIC的表SONIC就被导出 2、IMP: 具有三种模式(完全、用户...
在Oracle数据库中,死锁是由于多个并发操作试图同时锁定同一资源导致的。一旦发生死锁,Oracle会自动检测并解除其中一个事务的锁定,以确保其他事务可以继续执行。了解如何识别和解决Oracle中的查询死锁对于维护...
4. **字段长度限制**:在`VARCHAR2`类型中,我们指定了字段的最大长度,如`VARCHAR2(100)`表示该字段最多能存储100个字符。 5. **日期和时间处理**:Oracle支持多种日期和时间格式,使用`DATE`类型可以同时存储日期...
### Oracle过程中创建视图实例 在Oracle数据库管理中,通过PL/SQL块执行动态SQL语句是一项非常重要的技能。其中一个核心功能就是`EXECUTE IMMEDIATE`,它允许开发者在运行时构建并执行SQL语句。这在需要动态生成SQL...
【描述】:本文主要探讨了如何通过Oracle数据库的事件触发器增强“军卫一号”医院信息系统的安全性,针对系统中存在的用户权限管理漏洞提出解决方案。 【标签】:Oracle数据库、关系型数据库、参考文献、专业指导 ...
在Oracle ADF中,安全性通常涉及到对资源的访问控制,包括JSP(JavaServer Pages)页面。本文将深入探讨如何在Oracle ADF环境中配置不受保护的JSP页面,以便允许所有用户访问。 首先,了解Oracle ADF Security的...
Duplicate技术的核心优势在于它可以快速地复制主数据库到备用数据库,并且能够在复制过程中保持业务正常运行,这为实现高效的数据保护提供了一种有效途径。 #### 三、环境配置与准备 1. **网络配置**:在搭建...
在虚拟机上安装Oracle数据库是一个涉及多个步骤的过程,该教程旨在指导用户如何在虚拟机环境中设置Oracle数据库,并配置相关环境,使得用户能够在自己的主机上通过sqldeveloper这样的客户端工具连接到虚拟机中配置的...
本文详细介绍的oracle数据库如何存储bfile
在 Oracle 数据库中,经常会遇到需要导出某用户所有表的情况,这可以通过使用 exp 工具来实现。但是,这个命令需要指定具体的表名,这样就需要用户手动输入每个表名,这非常不方便。下面我们来介绍一个可以批量导出...