title: Postgresql / Hibernate - Large Objects may not be used in auto-commit mode
body: <p>Recently I migrated my application from <code>Oracle</code> to <code>PostgreSql</code> (9.2.20). I used <code>org.hibernate.type.MaterializedBlobType</code> for storing document contents (<code>bytes</code> property) in DB:</p>
  
  <pre><code>&lt;hibernate-mapping &gt;
    &lt;class name="x.y.z.entities.DocumentDataEntity" table="ds_document_data"&gt;
      &lt;id name="id" column="id_" type="java.lang.String"&gt;
        &lt;generator class="uuid2"/&gt;
      &lt;/id&gt;
      &lt;property name="bytes" column="BYTES_" type="org.hibernate.type.MaterializedBlobType"/&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><code>bytes</code> property in <code>DocumentDataEntity</code> class: </p>
  
  <pre><code>private byte[] bytes;
  </code></pre>
  
  <p>It works fine in <code>Oracle</code> but in <code>PostgreSql</code>, loading <code>DocumentDataEntity</code> throws this exception: </p>
  
  <pre><code>18:54:57 ERROR [ds/root] i.a.w.c.d.e.ApplicationExceptionHandler -| Error occured in calling rest method! org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
      at org.postgresql.largeobject.LargeObjectManager.open(LargeObjectManager.java:265)
      at org.postgresql.largeobject.LargeObjectManager.open(LargeObjectManager.java:251)
      at org.postgresql.jdbc.AbstractBlobClob.getLo(AbstractBlobClob.java:283)
      at org.postgresql.jdbc.AbstractBlobClob.getBinaryStream(AbstractBlobClob.java:127)
      at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:115)
      ... 163 common frames omitted
  Wrapped by: org.hibernate.HibernateException: Unable to access lob stream
      at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:118)
      at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:26)
      at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$1.doExtract(BlobTypeDescriptor.java:48)
      at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234)
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224)
      at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300)
      at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2790)
      at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.loadFromResultSet(EntityReferenceInitializerImpl.java:306)
      at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:233)
      at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:103)
      at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:239)
      at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
      at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
      at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
      at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4019)
      at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
      at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
      at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
      at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:116)
      at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
      at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
      at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:997)
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:157)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266)
      at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
  </code></pre>
  
  <p><strong>Note:</strong> </p>
  
  <ul>
  <li>I'm using <code>Spring</code> (4.3.8.RELEASE) and <code>Hibernate</code> (5.0.12.Final).</li>
  <li>I used <code>@Transactional</code> annotaion in <code>DocumentDataManager</code> bean's <code>load</code> method.</li>
  <li>Saving/Loading works fine in <code>Oracle</code>.</li>
  </ul>