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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With