hibernate 3.5+ 后的BLOB操作cannot be cast to oracle.sql.BLOB

如果你以前在创建blob类型的实体时,你一定会像这样

private Blob image;

其中Blob是java.sql.Blob

在操作Blob对象的时候会用到SerializableBlob这个类

但是当你的hibernate3.5后org.hibernate.lob.SerializableBlob这个类会被取消掉,取而代之的是org.hibernate.jdbc.ResultSetWrapper

具体:https://community.jboss.org/wiki/JDBC4Support

将之交给了JDBC4去处理

The first and simplest concern is the ability to compile Hibernate with JDK 1.6.  The issue here is that Hibernate implements a number of JDBC contracts which have changed.  This includes

  1. *internal* implementations of JDBC contracts
    1. org.hibernate.jdbc.ResultSetWrapper
  2. *exposed* implementations of JDBC contracts
    1. org.hibernate.lob.BlobImpl
    2. org.hibernate.lob.ClobImpl
    3. org.hibernate.lob.SerializableBlob
    4. org.hibernate.lob.SerializableClob

或者Hibernate.createBlob(new byte[1]);也被丢弃


你可能会遇到这样的异常:$Proxy115 cannot be cast to oracle.sql.BLOB

java.sql.Blob 不能转变为 oracle.sql.BLOB

解决方法:

第一种:就是在使用hibernate时,不直接通过hibernate的映射的setter和getter方法去操作bean或者entity,这样遇到java.sql.Blob类型无法转换为oracle.sql.BLOB

具体 :http://www.blogjava.net/rrong-m/archive/2009/06/15/282457.html

本人试后发现并不能很好的解决问题

第二种:hibernate3.5后在建实体或者bean是要将oracle中的BLOB字段映射成byte[]

private byte[] photo;

在action中对blob字段的保存

//hibernate 3.5后oracle BLOB类型需要byte[]声明 
try {
            InputStream in = new FileInputStream(image);
            byte[] photo = new byte[in.available()];
            in.read(photo);
            in.close();
            entity.setPhoto(photo);
        } catch (FileNotFoundException e) {            
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
在hibernate中blob被映射成byte[]

你可能感兴趣的:(hibernate 3.5+ 后的BLOB操作cannot be cast to oracle.sql.BLOB)