如果你看了前面的三篇文章也许会觉得自己已经掌握透了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.