Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load ManyToOne Relation with Hibernate Envers - Eager/Lazy?

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.

like image 374
java java Avatar asked Apr 24 '16 06:04

java java


People also ask

What is lazy loading and eager loading in hibernate with example?

3. Eager and Lazy Loading. The first thing that we should discuss here is what lazy loading and eager loading are: Eager Loading is a design pattern in which data initialization occurs on the spot. Lazy Loading is a design pattern that we use to defer initialization of an object as long as it's possible.

What is the difference between Fetchtype lazy and eager?

LAZY: It fetches the child entities lazily i.e at the time of fetching parent entity it just fetches proxy(created by cglib or any other utility) of the child entities and when you access any property of child entity then it is actually fetched by hibernate. EAGER: it fetches the child entities along with parent.

What is the difference between lazy and eager fetching in spring?

Lazy Loading vs. Eager Loading. While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed.


1 Answers

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.

like image 155
Naros Avatar answered Oct 10 '22 08:10

Naros