Search code examples
hibernatequarkusquarkus-panache

@Basic(fetch = FetchType.LAZY) @Lob with @EntityGraph is not eager fetched


Entity
@Table(name = "batch")
@NamedEntityGraph(name = "withLobs", attributeNodes = {
        @NamedAttributeNode(value = "log"),
        @NamedAttributeNode(value = "json"),
})
public class Batch extends PanacheEntityBase
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "HIBERNATE_SEQUENCE")
    @GenericGenerator(name = "HIBERNATE_SEQUENCE", strategy = "native")
    public long id;
        // other properties
    @Basic(fetch = FetchType.LAZY)
    @Lob
    public byte[] json;
    @Basic(fetch = FetchType.LAZY)
    @Lob
    @Column(name = "`log`")
    public byte[] log;
}

does works as expected (json and log are loaded with lazy with an extra query).
Applying an entitygraph:

final List<Batch> b = Batch.findAll(Sort.descending("creationDate"))
  withHint(QueryHints.HINT_FETCHGRAPH, Panache.getEntityManager().getEntityGraph("withLobs"))
page(Page.of(pagination.page - 1, pagination.limit)).list();

json and log are not included in main select and are always read using +1 select.
I tried adding quarkus.hibernate-orm.unsupported-properties."hibernate.bytecode.allow_enhancement_as_proxy"=true in application.properties as

<plugin>
  <groupId>org.hibernate.orm.tooling</groupId>
  <artifactId>hibernate-enhance-maven-plugin</artifactId>
  <executions>
    <execution>
      <configuration>
        <failOnError>true</failOnError>
        <enableLazyInitialization>true</enableLazyInitialization>
      </configuration>
      <goals>
        <goal>enhance</goal>
      </goals>
    </execution>
  </executions>
</plugin>

in pom.xml but unsuccessfully (but I'm quite sure bytecode enhance is already active in Quarkus).
Any advice?


Solution

  • You don't need quarkus.hibernate-orm.unsupported-properties."hibernate.bytecode.allow_enhancement_as_proxy"=true nor hibernate-enhance-maven-plugin when using Quarkus. Enhancement is done automatically by Quarkus. So that's not the problem.

    Depending on the version you're using, it might simply be a bug. Entity graph support in Hibernate ORM 5 is... patchy. Hibernate ORM 6 overhauled that part of the code and is much more reliable when it comes to entity graphs. Try again with Quarkus 3.0.0.Beta1 (which uses Hibernate ORM 6.2.0.CR4), and if that still doesn't work, you should report a bug: https://hibernate.atlassian.net/browse/HHH