-->
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.  [ 3 posts ] 
Author Message
 Post subject: Loading a lazy collection association on demand efficiently
PostPosted: Thu Oct 14, 2004 10:13 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hey. I have read the HTML manual and bought Hibernate in Action and I am still none the wiser about exactly the best thing to do for a basic Class A has a List collection. By default we do not want to load the list. At some point we decide to load Class A and we now want the collection loaded. The following code below is the only solution that I have tried that works but it is described in Hibernate in Action as inefficient (immediate mode) without going into how to solve it in practice.

Thanks for any insight!

Hibernate version:
2.1.4
Mapping documents:
VERY long! Here is the part I am asking about:

Code:
    <class name="com.qas.newmedia.intranet.pof.dto.POF"
      table="tbl_POFs">

        <id name="id" type="int" column="pof_id" unsaved-value="0">
           <generator class="identity" />
       </id>

      <list name="lineItems" inverse="true" lazy="true" cascade="all">
            <key column="pof_id"/>
         <index column="line_item_id" />
            <one-to-many class="com.qas.newmedia.intranet.pof.dto.LineItem" /> 
   </class>


Code between sessionFactory.openSession() and session.close():

Code:
         Session session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();
               
         pof= (POF) session.load(POF.class, id);
         
         List list = session.find(
            "select elements(pof.lineItems) from POF as pof where pof.id = ?",
            id,
             Hibernate.INTEGER
         );
         
         pof.setLineItems(list);
                  
         tx.commit();
         HibernateUtil.closeSession(session);


Full stack trace of any exception that occurs:
N/A

Name and version of the database you are using:
SQL Server 2000

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:
N/A


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 14, 2004 11:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Perhaps this code is overly simplified for posting, but in this case you immediately use the collection after loading its owner. Instead, for this particular use case you should eagerly fetch the associated collection. This is accomplished through HQL:
Code:
         // Notice the "fetch" keyword; it is important here
         String loadFetchQuery =
                 "select p " +
                 "from POF as p " +
                 "    left join fetch p.lineItems " +
                 "where p.id = :pofId";

         Session session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();

         pof = (POF) session.createQuery(loadFetchQuery)
                 .setInteger("pofId", id)
                 .uniqueResult();

         // At this point, POF.lineItems is fully loaded because of the "fetch"
         // and it was acheived extremely efficiently as it was loaded from
         // a single SQL SELECT using outer joining...

         tx.commit();
         HibernateUtil.closeSession(session);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 14, 2004 11:11 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
you're a genius .. exactly what i was looking for and works perfectly. great stuff, thanks!


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