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.

java java
  • 405
  • 1
  • 11
  • 25
  • 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? – LearningPhase Apr 24 '16 at 07:56
  • 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 ? – java java Apr 24 '16 at 10:16
  • There is an hibernate issue stating it won't be fixed :( https://hibernate.atlassian.net/browse/HHH-3552 – destan Oct 27 '16 at 20:03

1 Answers1

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.

Naros
  • 19,928
  • 3
  • 41
  • 71