We are having a similar problem after moving from Hibernate 3.3 to 3.6.
The mapping for the blob property looks like this:
<property
name="attachmentBlob"
type="java.sql.Blob"
column="ATTACHMENT_BLOB"
not-null="true"
length="4000"
/>
Here is example code that creates and saves the Blob:
org.hibernate.Session session = // get a hibernate session
File file = new File("/tmp/foo.png");
Blob attachmentBlob = session.getLobHelper().createBlob(new FileInputStream(file), file.length());
emailAttachment.setAttachmentBlob(attachmentBlob);
session.save(emailAttachment);
session.flush();
This results in:
java.lang.ClassCastException: $Proxy6 cannot be cast to oracle.sql.BLOB
at oracle.jdbc.driver.OraclePreparedStatement.setBlob(OraclePreparedStatement.java:6890)
at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$1.doBind(BlobTypeDescriptor.java:65)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:89)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:282)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2166)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2412)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2856)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
The attachmentBlob is an instance of class org.hibernate.engine.jdbc.BlobProxy, evidently the Hibernate engine tries to cast this to a native Blob type and it fails. We are using Oracle 10.
This used to work with Hibernate 3.3 and seems to be about as simple as can be. What is the correct way to do this?
A related question, this link:
http://www.hibernate.org/56.htmlis referenced in other forums and is supposed to have information about blobs. But the link no longer works, where can this information now be found?
Thanks in advance for any help.