`
fengzl
  • 浏览: 215589 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

blob、clob字段小结(四)

阅读更多
如果你看了前面的三篇文章也许会觉得自己已经掌握透了blob和clob的应用,而且所有测试程序都跑过了,没有问题啊,对于这个主题还有什么可讲的啊,那就让我用最后两篇来告诉你真正项目中会遇到的问题。

首先前面的hibernate中的应用是针对hibernate2.x的,现在3.x中对blob和clob增加了 org.hibernate.lob.SerializableBlob和org.hibernate.lob.SerializableClob类的封装。

其次如果你将前面的测试程序放到weblogic的容器中通过weblogic的数据源得到连接的话,你会发现 oracle.sql.BLOB blob = (oracle.sql.BLOB)person.getImage();和 oracle.sql.CLOB clob = (oracle.sql.CLOB)person.getArticle();这俩行会出错,原因就是weblogic进行了包装。

现在将以上两个问题的综合解决方案用以下代码说明:


            for (int i = 0; i < 10; i++) {
                LargeObject large = new LargeObject();
                large.setId(i + "");
                large.setName("林意炜");

                // 插入一个小数据数据
                large.setImage(Hibernate.createBlob(new byte[1]));
                large.setArticle(Hibernate.createClob(" "));

                session.save(large);
                session.flush();

                // 锁定该记录
                session.refresh(large, LockMode.UPGRADE);

                // 插入图片数据
                String fileName = "E:/AAA/" + i + ".jpg";
                SerializableBlob sb = (SerializableBlob)large.getImage();
                java.sql.Blob wrapBlob = sb.getWrappedBlob();
                // 通过非weblogic容器中数据源获得连接的情况
                if(wrapBlob instanceof oracle.sql.BLOB){
                    oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapBlob;
                    OutputStream out = blob.getBinaryOutputStream();
                    out.write(getData(fileName));
                    out.close();
                }
                // 使用weblogic的Oracle Thin driver类型连接池,驱动类名:oracle.jdbc.OracleDriver
                else if(wrapBlob instanceof weblogic.jdbc.vendor.oracle.OracleThinBlob){
                    OracleThinBlob blob = (OracleThinBlob)wrapBlob;
                    OutputStream out = blob.getBinaryOutputStream();
                    out.write(getData(fileName));
                    out.close();
                }


                // 插入文章数据
                fileName = "E:/AAA/" + i + ".java";
                SerializableClob cb = (SerializableClob)large.getArticle();
                java.sql.Clob wrapClob = cb.getWrappedClob();
                // 通过非weblogic容器中数据源获得连接的情况
                if(wrapClob instanceof oracle.sql.CLOB){
                    oracle.sql.CLOB clob = (oracle.sql.CLOB) wrapClob;
                    Writer writer = clob.getCharacterOutputStream();
                    String article = new String(getData(fileName));
                    writer.write(article);
                    writer.close();
                }
                // 使用weblogic的Oracle Thin driver类型连接池,驱动类名:oracle.jdbc.OracleDriver
                else if(wrapClob instanceof weblogic.jdbc.vendor.oracle.OracleThinClob){
                    OracleThinClob clob = (OracleThinClob)wrapClob;
                    Writer writer = clob.getCharacterOutputStream();
                    String article = new String(getData(fileName));
                    writer.write(article);
                    writer.close();
                }
            }




最后用引用两端摘自weblogic和hibernate网站的两段相关的话你可以读读:

Package Change for Oracle Thin Driver 9.x and 10g
For Oracle 8.x and previous releases, the package that contained the Oracle Thin driver was oracle.jdbc.driver. When configuring a JDBC connection pool that uses the Oracle 8.1.7 Thin driver, you specify the DriverName (Driver Classname) as oracle.jdbc.driver.OracleDriver. For Oracle 9.x and 10g, the package that contains the Oracle Thin driver is oracle.jdbc. When configuring a JDBC connection pool that uses the Oracle 9.x or 10g Thin driver, you specify the DriverName (Driver Classname) as oracle.jdbc.OracleDriver. You can use the oracle.jdbc.driver.OracleDriver class with the 9.x and 10g drivers, but Oracle may not make future feature enhancements to that class.


See the Oracle documentation for more details about the Oracle Thin driver. Note: The package change does not apply to the XA version of the driver. For the XA version of the Oracle Thin driver, use oracle.jdbc.xa.client.OracleXADataSource as the DriverName (Driver Classname) in a JDBC connection pool.



Blob and Clob support
Hibernate now wraps Blob and Clob instances, to allow classes with a property of type Blob or Clob to be detached, serialized, deserialized, and passed to merge(). However, this means that the Blob or Clob cannot be cast to a vendor specific type (eg. oracle.sql.CLOB). You must use the getWrappedClob() or getWrappedBlob() methods:

clob = (oracle.sql.CLOB) ( (org.hibernate.lob.SerializableClob) foo.getText() ).getWrappedClob();

We expect that this kind of thing will no longer be necessary when Oracle fixes their JDBC driver.
分享到:
评论

相关推荐

    Hibernate操作Oarcle中Clob、Blob字段小结

    这篇博客文章“Hibernate操作Oracle中Clob、Blob字段小结”可能会探讨如何在Hibernate中有效地处理这两种类型的数据。 1. **Clob与Blob的理解**: - Clob:Clob是用于存储大量字符数据的类型,例如长篇文章、XML...

    关于Clob类型在Hibernate中 的应用小结

    在Hibernate的XML映射文件中,你需要为这个Clob字段指定映射规则,例如: ```xml &lt;column name="SUMMARY_CLOB" /&gt; ``` **CRUD操作**: 4.2.1 **创建(Create)**: 在创建新记录时,你需要将待存入的文本...

    关于Clob类型在Hibernate中 的应用小结.rar

    通常,要解决超过4000字节的数据,一种做法是将数据写入文件,xml或plain file都可以,数据...另一个做法是使用clob, blob等字段类型,主要有:采用传统的jbdc方式、把clob以string方式处理、直接使用clob类型三种方案

    关于Clob类型在Hibernate中 的应用小结-- 一篇使用心得.zip

    3. **插入与更新**:当向数据库中插入或更新包含Clob字段的记录时,Hibernate会自动处理这些数据。可以使用`session.save()`或`session.update()`方法,但在实际使用中,我们通常需要使用`session.merge()`,因为它...

    EJB3.0注释小结

    6. **@Lob** 和 **@Basic(fetch=FetchType.LAZY)**:结合使用表示字段是大对象(LOB,如Clob或Blob),并且以懒加载的方式获取。 7. **@PersistenceContext**:在SessionBean中注入EntityManager,以管理实体的生命...

    MySQL 建表的优化策略 小结

    - **BLOB和CLOB**:这些大对象类型不宜建立索引,因为它们对性能影响较大,且索引效果不佳。 6. **特殊优化策略**: - **表格分割**:通过分区或分表,将大表拆分成更小的部分,以分散I/O负载,提高查询效率。 -...

    Java数据库编程宝典2

    1.6 小结 第2章 设计数据库 2.1 数据库设计应考虑的事项 2.1.1 项目规范 2.1.2 设计表 2.1.3 生成发票 2.2 引用完整性 2.2.1 通用完整性规则 2.2.2 特定于数据库的完整性规则 2.3 小结 第3章 SQL基础 ...

    Java数据库编程宝典4

    1.6 小结 第2章 设计数据库 2.1 数据库设计应考虑的事项 2.1.1 项目规范 2.1.2 设计表 2.1.3 生成发票 2.2 引用完整性 2.2.1 通用完整性规则 2.2.2 特定于数据库的完整性规则 2.3 小结 第3章 SQL基础 ...

    Java数据库编程宝典1

    1.6 小结 第2章 设计数据库 2.1 数据库设计应考虑的事项 2.1.1 项目规范 2.1.2 设计表 2.1.3 生成发票 2.2 引用完整性 2.2.1 通用完整性规则 2.2.2 特定于数据库的完整性规则 2.3 小结 第3章 SQL基础 ...

    Java数据库编程宝典3

    1.6 小结 第2章 设计数据库 2.1 数据库设计应考虑的事项 2.1.1 项目规范 2.1.2 设计表 2.1.3 生成发票 2.2 引用完整性 2.2.1 通用完整性规则 2.2.2 特定于数据库的完整性规则 2.3 小结 第3章 SQL基础 ...

    数据库建表语句

    ### 五、小结 数据库建表语句是数据库设计与管理的基础,尤其是在Oracle这样的关系型数据库管理系统中。通过理解和掌握正确的建表语法,可以有效地创建满足业务需求的数据库表,进而提升数据处理的效率和准确性。在...

    ORACLE设计表时

    ### 小结 综上所述,在设计Oracle表时,针对不同数据类型,应根据实际需求选择合适的类型和长度。对于Integer类型,可以根据数据范围选择NUMBER或INTEGER;Long类型已被废弃,建议使用CLOB或BLOB替代;Date类型则...

    hibernate annotation学习文档

    #### 五、小结 通过上述示例,我们了解了如何使用 Hibernate 注解来快速实现 Java 对象与数据库表之间的映射。这些注解大大简化了实体类的定义,同时也提供了足够的灵活性来处理复杂的业务需求。对于初学者而言,...

    Spring中文帮助文档

    14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...

    Spring API

    14.5.2. 小结 14.6. 文档视图(PDF/Excel) 14.6.1. 简介 14.6.2. 配置和安装 14.7. JasperReports 14.7.1. 依赖的资源 14.7.2. 配置 14.7.3. 构造ModelAndView 14.7.4. 使用子报表 14.7.5. 配置Exporter的...

Global site tag (gtag.js) - Google Analytics