David.Turing's blog

 

Oracle Blob字段写入时产生转型异常Cast Exception

我用Hibnernate(JDBC太麻烦了)写图片到Blob字段,产生转型异常,
       Configuration config =  new Configuration().configure();
//       config.addClass(TSealTemplate.class);
       SessionFactory sf= config.buildSessionFactory();
        //SessionFactory sf = HibernateSessionFactory.getSessionFactory();
        s = sf.openSession(); 
        Transaction tx = s.beginTransaction();
        TSealTemplate c = new TSealTemplate();
        c.setUserid("USER0001");
        c.setSealTemplBlob(Hibernate.createBlob(buffer));
        s.save(c);
        s.flush();
        s.refresh(c, LockMode.UPGRADE);

       BLOB blob = (BLOB) c.getSealTemplBlob();

关于此问题在JavaEye上有一篇文章讨论,原因是
java.sql.Blob不能强制传唤成oracle.sql.BLOB

解决方法如下:


        SerializableBlob blob=(SerializableBlob)c.getSealTemplBlob();
        BLOB blob2 = (BLOB)blob.getWrappedBlob();    
        OutputStream out = blob2.getBinaryOutputStream();   

posted on 2006-02-07 13:29 david.turing 阅读(1999) 评论(2)  编辑  收藏

评论

# re: Oracle Blob字段写入时产生转型异常Cast Exception 2006-02-09 15:19 david.turing

同样,对Clob,也是这样转型

SerializableClob clob=(SerializableClob)m_sig.getSignValue();

CLOB clob2 = (CLOB)clob.getWrappedClob();

Writer out = clob2.getCharacterOutputStream();
out.write(sig);
out.close();  回复  更多评论   

# re: Oracle Blob字段写入时产生转型异常Cast Exception 2009-09-19 14:31 ccccc

jndi 下的转换
public int setBytes(long pos, byte[] bytes) throws SQLException
{
if (blob != null)
{
if (blob.getClass().getName().toLowerCase().indexOf("weblogic") >= 0)
{
try
{
Method method = blob.getClass().getMethod("putBytes",new Class[]{long.class,byte[].class});

return ((Integer)method.invoke(blob, new Object[]{pos, bytes})).intValue();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

//[weblogic]
// if (blob instanceof weblogic.jdbc.vendor.oracle.OracleThinBlob)
// {
// weblogic.jdbc.vendor.oracle.OracleThinBlob oBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) blob;
// return oBlob.putBytes(pos, bytes);
// }
//[endweblogic]
//[websphere]

if (blob instanceof oracle.sql.BLOB)
{
oracle.sql.BLOB oBlob = (oracle.sql.BLOB) blob;
return oBlob.putBytes(pos, bytes);
}
//[endwebsphere]

return this.blob.setBytes(pos, bytes);
}  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(110)

我参与的团队

随笔分类(126)

随笔档案(155)

文章分类(9)

文章档案(19)

相册

搜索

积分与排名

最新随笔

最新评论

阅读排行榜

评论排行榜