Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force eager loading of otherwise lazy loaded properties

I've got a Hibernate object which's properties are all loaded lazy. Most of these properties are other Hibernate objects or PersistentSets.

Now I want to force Hibernate to eager load these properties for just one time.

Of course I could "touch" each of these properties with object.getSite().size() but maybe there's another way to achieve my goal.


2 Answers

This is an old question, but I also wanted to point out the static method Hibernate.initialize.

Example usage:

Person p = sess.createCriteria(Person.class, id);
Hibernate.initialize(p.getChildren());

The children are now initialized to be used even after the session is closed.

like image 178
stephen.hanson Avatar answered Sep 12 '25 10:09

stephen.hanson


The documentation puts it like this:

You can force the usual eager fetching of properties using fetch all properties in HQL.

References

  • Hibernate Core Reference Guide
    • 19.1.7. Using lazy property fetching
like image 31
Pascal Thivent Avatar answered Sep 12 '25 08:09

Pascal Thivent