我用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();