Load ManyToOne Relation with Hibernate Envers - Eager/Lazy?
Asked Answered
I

1

6

I use Hibernate Envers 4.3.10.Final. I have the following two JPA-classes:

public class Factory {
     private int factoryID;
     .... 
}

public class Trgs{
     private int trgsID;

     @ManyToOne(fetch=FetchType.EAGER)
     @JoinColumn(name="fk_factory")
     private Factory factory;
}

I wrote a method that retuns all Audited Trgs Objects.

The method is :

public List<Trgs> readAuditedTrgs (List<Integer> trgsIds) {
      AuditReader reader = AuditReaderFactory.get(entityManager);
      AuditQuery query = reader.createQuery().forRevisionsOfEntity(Trgs.class, true, true);

      query.add(AuditEntity.id().in(ids));
      query.add(AuditEntity.revisionType().eq(RevisionType.ADD));
      query.addOrder(AuditEntity.revisionNumber().desc());
      return  query.getResultList() ;
}

After executing the Method above, my result is a Audited Trgs List. Each Trgs Object has of course the correct and releated Audited Factory Object.

But the Problem is, that i have learned that Hibernate Envers always loads Relation LAZY.

So in my Case i have to iterate over the Trgs List and initialize each Factory Object.

 for (Trgs trgs : resultList) {
      Hibernate.initialize(trgs.getFactory());
    }

So if i had for example 300 Trgs Objects, i have to initialize 300 Factory Objects. And that costs sooooo much. I have to wait One Minute.

I have learned that it is not possible to Load The Factory Object Eagerly. But i need an other solution. I show this Data in a Dashboad Site (Web Project). The user can not wait one Minute until the data are loading.

Please Help me to solve this Problem. Thanks.

Importation answered 24/4, 2016 at 6:51 Comment(3)
If you have mentioned fetch type as eager it should load it right away. Are you asking if you change it to lazy then the above mentioned thing will happen?Most
No.On the Hibernate Envers documentation it is explained that the Relations are always LAZY, no matter which FetchType I have declared. So in my case i have declared the Factory Object Eager, but hibernate Envers loads the Factory Lazy anyway. Have you understand my Explanation ?Importation
There is an hibernate issue stating it won't be fixed :( hibernate.atlassian.net/browse/HHH-3552Wooldridge
A
2

There are a number of improvements that the Envers AuditQuery API could use, this likely being a good example of its own short-comings. I have added HHH-11479 to JIRA to track this feature.

Amy answered 9/2, 2017 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.