I'm trying to create blob via Hibernate 4.3.5 API. H2 1.4.190 in-memory. Originally code was written for Oracle DB and now we are trying to use H2 for unit testing.
Blob blob = Hibernate.getLobCreator(hibernateSession).createBlob(new byte[]{0});
blob.setBinaryStream(1);
During createBlob() stack is following:
at org.h2.value.ValueLobDb.<init>(ValueLobDb.java:75)
at org.h2.value.ValueLobDb.createSmallLob(ValueLobDb.java:667)
at org.h2.value.ValueLobDb.createSmallLob(ValueLobDb.java:654)
at org.h2.store.LobStorageMap.createBlob(LobStorageMap.java:162)
at org.h2.jdbc.JdbcConnection.createBlob(JdbcConnection.java:1826)
at org.h2.jdbc.JdbcBlob.setBytes(JdbcBlob.java:124)
at org.hibernate.engine.jdbc.ContextualLobCreator.createBlob(ContextualLobCreator.java:68)
And precision is set to 1 in constructor (because of single byte array used as createBlob argument?). Next setBinaryStream(1) throws exception.
Am I doing something incorrectly or is this H2 issue?
Ok posting the answer for someone who finds it useful:
Apparently as I was told here:
For some reason Hibernate does not seem to have an API to create a proper empty Blob and it is only legal to call setBinaryStream on an empty Blob
So I had to:
Blob blob = Hibernate.getLobCreator(hibernateSession).createBlob(new byte[0]);
blob.setBinaryStream(1);