-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Lazy Loading for Collection Only
PostPosted: Fri Dec 30, 2011 12:02 pm 
Newbie

Joined: Wed Nov 10, 2010 4:24 pm
Posts: 6
I'm trying to setup my hibernate mappings to load with a distinct behavior in mind. I would like all properties, which are not Collections/Explicit Proxies, to load immediately and have the remainder collections load when I call Hibernate.initialize. I figured I could manage this by setting lazy=false and overiding this setting to lazy for all collections. For example:

Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = false)
@Table(name = "OBJECT_T")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SimObject implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PERSIST_ID", nullable = false)
    private Long id = new Long(0);

    @Column(name = "PERSIST_LOG_TIME", nullable = false)
    private Date logTime = new Date();

    @Column(name = "CONFIG_NAME")
    private String configName = "";

    @ManyToOne(fetch = FetchType.LAZY)
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
        CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
    @JoinColumn(name = "EQUIP_INFO_FK")
    private SimEquipmentInfo info = null;

    @OneToMany(fetch = FetchType.LAZY)
    @Cascade({CascadeType.DELETE, CascadeType.SAVE_UPDATE, CascadeType.PERSIST,
        CascadeType.MERGE,CascadeType.REFRESH, CascadeType.DETACH})
    @JoinTable(name = "SIM_TEST_MAP_T", joinColumns = {
            @JoinColumn(name = "TEST_ID")
    }, inverseJoinColumns = {
            @JoinColumn(name = OTHER_ID")
    })
    private final Set<SimTest> test = new HashSet<SimTest>(0);
...


To retrieve initial resultset:

Code:
            Query query = hsession.createQuery(strHQL);

            results = query.list();


Now,, when I need further data, the collections, just call:

Code:
Hibernate.initialize(o);


Note, this is of course done with the session open.

Now, with these settings in mind, I call Hibernate.initialize(object) and the equipment info and test collection don't initialize. I've read through the doc, http://docs.jboss.org/hibernate/core/4. ... ialization, but it doesn't seem very clear how the 'initialize' method operates under different fetching strategies. It's also not very clear how the 'initiailize' method loads parent/child relationships. After reading the doc, it seems my understand is initialize should load all parent/child data. However, this is not the case for the example above. None of the lazy properties get loaded. I understand I could drill down and call Hibernate.Initialize(parent.getChild()), but why would I even use initialize for that when the getter will fetch the data automatically. What exactly is the advantage of this Hibernate.Initialize method?

What I would really like to achieve here is having properties load by default with the exception of collections and any proxy objects i flag 'lazy'. I don't have the luxury of keeping long sessions open here, nor do I want to continually call n+1 queries everytime i hit a getter method. I can reattach/merge if needed, but I don't want a ton of long sessions open throughout the life of my program. Any info/advise for this use-case is greatly appreciated!


Top
 Profile  
 
 Post subject: Re: Lazy Loading for Collection Only
PostPosted: Fri Dec 30, 2011 1:31 pm 
Newbie

Joined: Wed Nov 10, 2010 4:24 pm
Posts: 6
Ok, well I found a work around. Seems like initialize should have an option to traverse the parent and initialize all. I've tried using HQL with "fetch all properties", but this only brings back proxies. I'm not quite sure why this exists as a proxy return is already performed. Regardless, I am able to traverse the class properties checking if initialized and forcing each property to load:

Code:
            for (Object o:results) {
                Field[] fields=o.getClass().getDeclaredFields();

                for (Field field:fields) {
                    field.setAccessible(true);

                    Object objectToInitialize = field.get(o);

                    boolean isInited=Hibernate.isInitialized(objectToInitialize);

                    if (!isInited) {
                        Hibernate.initialize(objectToInitialize);
                    }
                }
            }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.