Does anyone know hibernate version 4.2.3.Final changed the way of version 4.1.7.Final copying blob behavior?
Here is what I am running into trouble with 4.2.3.Final.
Environment:
Hibernate 4.2.3.Final and Oracle Oracle Database 11g Release 2 (11.2.0.3) JDBC Driver
Scenario:
The entity manager and a blob are defined in an entity class as
Code:
public class Attachment {
...
@PersistenceContext transiet EntityManager em;
...
@Basic(fetch=FetchType.LAZY)
@Column (insertable=true,updatable=false,name="document")
@Blob
private Blob document;
...
public InputStream getDocument() { return doc.getBinaryStream(); }
public void setDocument(InputStream is) {
Session hibnernateSession=(Session)em.getDelegate();
LobCreator creator=Hibernate.getLobCreator(hibernateSession);
this.document=createBlob(is, is.available());
}
...
}
Say, for example, I have an instance called oldAttachment fetched from the database,
then I newly create another instance called newAttachment.
When I want to copy a blob data from oldAttachment to newAttachment, under hibernate 4.1.7.Final, I can just simply do as:
Code:
newAttachment.setDocument( oldAttachment.getDocument() );
then save the newAttachment to the database, and it works perfectly.
Now under hibernate 4.2.3.Final, if I do the way as shown above, I will get an oracle error, which complains that I give a NULL value.
In other words, the hibernate does not read the blob stream data out from the oldAttachment and then put data into the newAttachment for me. Instead, it requires me (API user) do the actual copying stream thing.
In order to copy a blob from one the other, I have do something like:
Code:
{
...
BufferedInputStream bin = new BufferedInputStream( oldAttachment.getDocument() );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int len = 0;
while( (len=bin.read(data, 0, data.length) != -1 ) {
baos.write(data, 0, len);
}
baos.flush();
ByteArrayInputStream foo = new ByteArrayInputStream( baos.toByteArray() );
// then call
newAttachment.setDocument( foo );
// then call save newAttachment to database etc.....
...
}
As you can see the 4.2.3.Final version makes the user's life not easier, but harder. So I am wondering if it is a bug in the hibernate's BlobProxy or a feature of hibernate 4.2.3.Final.
I'd like to know what's the rational behind that hibernate changed the copy blob behavior as it used to be.
Many many thanks if someone can explain to me!