Blobs, InputStream and Envers: Could not reset reader

Hello everyone,

I have a problem with Hibernate, Blobs, and Envers. I have an entity that has a java.sql.Blob mapped like this in a audited entity:

@Column(name = "ASSET_DATA")
@Basic(fetch = FetchType.LAZY)
private Blob data;

When I now try to update the entity with a file uploaded using RestEasy using this code:

    public String uploadFile(
        final String assetIdentifier
        final MultipartFormDataInput input
) {

    Asset asset = assetRepo.getAsset(assetIdentifier);

    final Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
    final List<InputPart> inputParts = uploadForm.get("fileData");
    for (final InputPart inputPart : inputParts) {
        try {
            Blob blob = BlobProxy.generateProxy(inputPart.getBody(InputStream.class, null), -1);
  ; // calls EntitiyManager#persist(asset);
        } catch(IOExpection ex) {
             // Exception handling omitted for this topic.

I then get the following exception (only the last two levels of stack trace shown here, if you need more please let me know):

Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [org.example.assets.Asset_AUD]
        at org.hibernate@5.3.20.Final//org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(
        at org.hibernate@5.3.20.Final//org.hibernate.persister.entity.AbstractEntityPersister.insert(
        at org.hibernate@5.3.20.Final//org.hibernate.persister.entity.AbstractEntityPersister.insert(
        at org.hibernate@5.3.20.Final//org.hibernate.action.internal.EntityInsertAction.execute(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.spi.ActionQueue.executeActions(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.spi.ActionQueue.executeActions(
        at org.hibernate@5.3.20.Final//org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(
        at org.hibernate@5.3.20.Final//org.hibernate.event.internal.DefaultFlushEventListener.onFlush(
        at org.hibernate@5.3.20.Final//org.hibernate.internal.SessionImpl.doFlush(
        ... 134 more
Caused by: java.sql.SQLException: could not reset reader
        at org.hibernate@5.3.20.Final//org.hibernate.engine.jdbc.BlobProxy.resetIfNeeded(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.jdbc.BlobProxy.getUnderlyingStream(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.jdbc.BlobProxy.getStream(
        at org.hibernate@5.3.20.Final//org.hibernate.engine.jdbc.BlobProxy.invoke(
        at org.hibernate@5.3.20.Final//com.sun.proxy.$Proxy309.getBinaryStream(Unknown Source)
        at deployment.postgresql.jar//org.postgresql.jdbc.PgPreparedStatement.setBlob(
        at org.jboss.ironjacamar.jdbcadapters@1.4.25.Final//org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setBlob(
        at org.hibernate@5.3.20.Final//org.hibernate.type.descriptor.sql.BlobTypeDescriptor$4$1.doBind(
        at org.hibernate@5.3.20.Final//org.hibernate.type.descriptor.sql.BasicBinder.bind(
        at org.hibernate@5.3.20.Final//org.hibernate.type.AbstractStandardBasicType.nullSafeSet(
        at org.hibernate@5.3.20.Final//org.hibernate.type.AbstractStandardBasicType.nullSafeSet(
        at org.hibernate@5.3.20.Final//org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(
        at org.hibernate@5.3.20.Final//org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(
        at org.hibernate@5.3.20.Final//org.hibernate.persister.entity.AbstractEntityPersister.insert(
        ... 141 more

It looks like the InputStream provided by InputPart does not support resetting, but Hibernate needs to reset the stream because it has to insert the data twice…

Any idea how to solve this? We would like to have the Blob versioned, so any help is appreciated.



It’s probably best to ensure you materialize the file in a temp file on the file system first to avoid a slow user connection block your transaction. Streaming within a transaction is never a good idea. So one way is to just create the temp file, write the stream into it and when that is finished, attach a blob that is based on a file input stream which is then resetable.

Doing that already. The error message is the same, but on further investigation, I found out that in that case the FileInputStream is closed by the BlobProxy before Hibernate tries to insert the data in the audit table.

If you think this is a bug in Hibernate Envers due to the blob being closed too early, please create an issue in the issue tracker( with a test case(hibernate-test-case-templates/ at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.

Just created a bug: [HHH-14724] Metamodel generates invalid model classes for converters and user types - Hibernate JIRA

A link to the test case is in the issue.

Thanks for looking into that.

Sorry, wrong link. Correct link to the Jira issue: [HHH-14725] Using a InputStream with BlobProxy and Envers results in java.sql.SQLException: could not reset reader - Hibernate JIRA