Re: Reading a Blob field using Hibernate
Fred Toussi via Hsqldb-user <[email protected]> Tue, 20 Aug 2019 14:45:43 +0100
| Newsgroups | gmane.comp.java.hsqldb.user |
|---|---|
| Message-ID | <[email protected]> |
Thanks for the code example Jeroen.
Code attached. I have modified it to support in-procee mem: and server-based mem: databases with two profiles.
The call to createBlob(stream, length) should specify the actual length of the stream. This call is translated to a JDBC call with the same parameters. I set the length to the length of your test string and it works fine in both profiles.
Specifying a longer string raises an internal exception in HSQLDB that is not properly handled during the creation of the blob. The blob is not initialised properly and causes a problem when accessed later. I will fix this issue and will probably allow a stream that is shorter than the specified length. This will be done in the next few days and committed for the next release.
See below for Hiberrnate method's JavaDoc
Re the Server settings, the server has an internal database URL (file: or mem:) paired with its alias, for each database that it serves. The alias is not a schema, as there can be multiple schemas within a single database.
Regards
Fred
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/engine/jdbc/LobCreator.java
/**
* Create a BLOB reference encapsulating the given binary stream.
*
* @param stream The binary stream to wrap as a blob.
* @param length The length of the stream.
* @return The created blob, castable to {@link Blob} as well as {@link BlobImplementer}
*/
public Blob createBlob(InputStream stream, long length);
On Mon, Aug 19, 2019, at 11:33, Jeroen van der Vegt wrote:
> Hi,
>
>
> The database was running all the time. I update my code to a fully working example.
>
> Using a file: database does change the error to this, but that isn't all that helpful:
>
> Caused by: org.hsqldb.HsqlException: file input/output error
> at org.hsqldb.error.Error.error(Unknown Source)
> at org.hsqldb.types.BlobDataID.getBytes(Unknown Source)
> at org.hsqldb.types.BlobInputStream.readIntoBuffer(Unknown Source)
>
>
> Kind regards,
> *Jeroen van der Vegt *Lead Designer
>
> *T* +31 1 82 59 40 00
> *A** *Burgemeester Jamessingel 1, P.O. Box 2013, 2800 BD Gouda, The Netherlands* | W* technolution.nl
>
> This email is intended exclusively for the addressee(s), and may not be passed on to, or made available for use by any person other than the addressee(s). Technolution B.V. rules out any and every liability resulting from any electronic transmission.
>
>
> Op ma 19 aug. 2019 om 09:57 schreef Fred Toussi via Hsqldb-user <[email protected]>:
>> __
>> Your code is for testing persistence. It works on Oracle, which is persistent, and fails on the HSQLDB mem: database, which is not persistent if the database is shutdown. Try using an HSQLDB file: database to get a closer match and report if there is still an issue.
>>
>> The stack trace shows the lob store is empty when you try to retrieve the lob. So in between saving the data and reading it back the database was probably closed.
>>
>> Regards
>>
>> Fred Toussi
>>
>> On Sun, Aug 18, 2019, at 18:30, Jeroen van der Vegt wrote:
>>> Hello all,
>>>
>>>
>>> I keep running into this exception, but I do not understand why as the code works fine using Oracle as a database.
>>> For this test, I'm using HSQLDB 2.5.0, Jdk 8.144 and Hibernate 5.2.18, with the default HSQLDialect in an in-memory HSQLDB database.
>>>
>>> I've attached some dummy java code which should make the code I'm using clear:
>>> First, there is a database entity class called 'MyAttachment', which stores binary data in a (java.sql.)Blob.
>>> For testing, I store a small amount of data ("this is a test".getBytes()) in an inputStream, and store that as an attachment (see the streamToDb-method). This seems to work fine, as the Hsqldb DatabaseManager shows a records with (binary data) in it.
>>>
>>> When I try to retrieve the data however, it fails as soon as I try to access the InputStream (see the getAttachmentData-method). When updating the code to read some bytes instead of accessing a stream, I get the same exception (though not wrapped):
>>>
>>> java.io.IOException: java.lang.IndexOutOfBoundsException: Index out of bounds: 0 >= 0
>>> at org.hsqldb.lib.java.JavaSystem.toIOException(Unknown Source)
>>> at org.hsqldb.types.BlobInputStream.read(Unknown Source)
>>> at hsqldb.Test.test(Test.java:53)
>>> ...
>>> Caused by: java.lang.IndexOutOfBoundsException: Index out of bounds: 0 >= 0
>>> at org.hsqldb.lib.HsqlArrayList.get(Unknown Source)
>>> at org.hsqldb.persist.LobStoreMem.getBlockBytes(Unknown Source)
>>> at org.hsqldb.persist.LobManager.getBytesNormal(Unknown Source)
>>> at org.hsqldb.persist.LobManager.getBytes(Unknown Source)
>>> at org.hsqldb.Session.performLOBOperation(Unknown Source)
>>> at org.hsqldb.Session.execute(Unknown Source)
>>> at org.hsqldb.types.BlobDataID.getBytes(Unknown Source)
>>> at org.hsqldb.types.BlobInputStream.readIntoBuffer(Unknown Source)
>>> ... 33 more
>>>
>>>
>>> What is going wrong? As it works with Oracle, I suppose the problem is Hsqldb-specific?
>>>
>>>
>>> Kind regards,
>>>
>>> *Jeroen van der Vegt *Lead Designer
>>>
>>> *T* +31 1 82 59 40 00
>>> *A** *Burgemeester Jamessingel 1, P.O. Box 2013, 2800 BD Gouda, The Netherlands* | W* technolution.nl
>>>
>>> This email is intended exclusively for the addressee(s), and may not be passed on to, or made available for use by any person other than the addressee(s). Technolution B.V. rules out any and every liability resulting from any electronic transmission.
>>>
>>> _______________________________________________
>>> Hsqldb-user mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/hsqldb-user
>>>
>>>
>>> *Attachments:*
>>> * hsqldb_example.java
>>
>> _______________________________________________
>> Hsqldb-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/hsqldb-user
>
> _______________________________________________
> Hsqldb-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/hsqldb-user
>
>
> *Attachments:*
> * TestBlob.java
_______________________________________________
Hsqldb-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hsqldb-user
TestHibernateBlob.java
(application/octet-stream, 10.1 KB) - not displayed