user3719630
user3719630

Reputation: 93

How to turn on eager initialization in Hibernate throught method calling

The default entities loading in hibernate is set to lazy. I would like to turn on the eager initalization in Hibernate for just while method calling and then still using lazy loading:

For example i have entity like:

 public class Applications  implements java.io.Serializable {


     private int id;
     private String name;
     private Set viewses = new HashSet(0); // here entities views
     private Set routeses = new HashSet(0);  // here antoher entities routes

     public Set getViewses() {
        return this.viewses;
     }

     public void setViewses(Set viewses) {
        this.viewses = viewses;
     }

     public Set getRouteses() {
        return this.routeses;
     }

     public void setRouteses(Set routeses) {
        this.routeses = routeses;
     }

     // .... other things

 } 

I want avoid to iterate all inners objects for completly get all details about object Applications For example:

        begin();
        List apps = getSession().createQuery("from Applications").list(); // here lazy initalisation not all inner elements are filled from database
        commit();

Above code resulting that inner entities like (routes,views) are empty. When i call application.getRouteses() nothing happens beacuse i have set lazy to true and i call getRouteses() after session close (commit) Only way to set all elementy in entitiy Application throught one transaction and return it completly from method is use iteration and setters or eager initialisation:

        begin();
        List apps = getSession().createQuery("from Applications").list();
        Iterator iter = apps.iterator();
        while(iter.hasNext()){
            Applications application = (Applications) iter.next();
            application.setRouteses(application.getRouteses()); // here i set all routes beacuse i call getRoutes and hibernate will load from db
        }
        commit();

Now eager inistalistation, below code load all details about object with inner routes and views entities too:

        begin();
        List apps = getSession().createQuery("from Applications").list(); // here eager all inner object are filled from databasse
        commit();

only problem is change lazy to eager but not in hibernate.xml configuration. I would like to know it is possible to trun on eager for some time and then turn on lazy (programatical). Fo example:

        // HERE LAZY INITIALISATION
        turnOnEager(); // some method ???

        // HERE EAGER INITIALISATION
        begin();

        List apps = getSession().createQuery("from Applications").list(); // here eager all inner object are filled from databasse
        commit();

        // AND LAZY AGAIN
        turnOnLazy(); // some method ???

Upvotes: 1

Views: 633

Answers (1)

Zeus
Zeus

Reputation: 6566

You may not turn on and off the lazy at runtime. But, you can try to change the hql to fetch the child entities like below using fetch keyword.

HQL

from Applications a join fetch a.someProperty s join fetch s.anotherProperty

From docs:

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.

Ref: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html

Upvotes: 1

Related Questions