-->
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.  [ 7 posts ] 
Author Message
 Post subject: Equivalent of Hibernate.initialize using JPA
PostPosted: Fri Jun 06, 2008 5:03 am 
Newbie

Joined: Wed May 18, 2005 4:50 am
Posts: 18
To initialize a lazy collection with Hibernate core one can use

Code:
Hibernate.initialize(parentEntity.getSetOfChildren());

I have been trying to find an "equivalent" method using only JPA/EntityManager but I can't seem to find any.

What is the "best" method to initialize a proxy set without explicitly using the Hibernate class?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 8:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I am not aware of an equivalent. You could try to load the entity and its associated collection using the Criteria API. There you can specify the fetch mode (FetchMode.JOIN) which will eagerly fill the collections even if they are mapped as lazy. If you need the collection loaded eagerly all the time why not map it eager? Or why not just rely on transparent lazy loading?
What is your actual use case?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 10:37 am 
Newbie

Joined: Wed May 18, 2005 4:50 am
Posts: 18
Thankyou for your reply.

I have been working on a Hibernate-Core-only project for some time now and have just started looking into JPA, trying to see where it's the same and where it's different.

In the Hibernate core project we are using the Hibernate.initialize() method in the following situations

- the Collection is rarely needed so mapping it eager is not a desired option
- transparent lazy loading is not an option as the entity is detached
- the method used to load the entity is using HQL for the query and using "left join fetch" on a One-To-Many relation will duplicate rows in the SQL ResultSet.

Our standard approach is as follows

- map almost everything "lazy"
- use explicit HQL for queries, using "left join fetch" for X-To-One relations
- depend on transparent lazy loading while the session is open
- use Hibernate.initialize() for X-To-Many relations, in case the Collections are used after detaching the objects

With this approach using "Hibernate.initialize()" seems logical and I thought something similar would be available in JPA.

Of course we can just access the Collection elements while the session is still open (e.g. getSetOfChildren().size()) for transparent lazy loading, but I think that a dedicated method like "Hibernate.initialize()" is the "cleaner" solution, as it clearly shows the intention.


        Top
         Profile  
         
         Post subject:
        PostPosted: Fri Jun 06, 2008 10:57 am 
        Hibernate Team
        Hibernate Team

        Joined: Thu Apr 05, 2007 5:52 am
        Posts: 1689
        Location: Sweden
        Sure, you can just call size() to initialize the collection. Why do you not create a project internal helper class/method with an appropriate name. this way your intentions become clear whereas internally you still can just call size().

        --Hardy


        Top
         Profile  
         
         Post subject:
        PostPosted: Fri Jun 06, 2008 3:30 pm 
        Newbie

        Joined: Wed May 18, 2005 4:50 am
        Posts: 18
        Sounds like a great idea.

        Thanks.


        Top
         Profile  
         
         Post subject:
        PostPosted: Sun Jun 08, 2008 10:46 am 
        Expert
        Expert

        Joined: Tue May 13, 2008 3:42 pm
        Posts: 919
        Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
        Great info, swillfah!

        _________________
        Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
        http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


        Top
         Profile  
         
         Post subject:
        PostPosted: Tue Jan 06, 2009 7:59 am 
        Newbie

        Joined: Tue Mar 11, 2008 4:49 am
        Posts: 12
        I tend to use Hibernate.initialize(obj.getSet()) call very often to load association objects that are to be presented on a page.
        The one problem i have is that i am not able to initialize set and its dependent associations using single Hibernate.initialize() call.

        So if set element class has X-2-one association i would like hibernate to load the set elements along with each associated object with 1 query.


        Top
         Profile  
         
        Display posts from previous:  Sort by  
        Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.