-->
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.  [ 6 posts ] 
Author Message
 Post subject: eager fetching lazy collection with criteria?
PostPosted: Wed Feb 04, 2009 11:04 pm 
Newbie

Joined: Wed Nov 05, 2008 1:17 pm
Posts: 7
I need to eager fetch a lazy collection with Criteria. The JPA book seems to indicate that I can use FetchMode.JOIN to override laziness on a collection and force it to load. This doesn't seem to be working for my example below:

Mapping file
Code:
<hibernate-mapping auto-import="true" default-lazy="true" package="com.orders" >

    <class name="Customer" table="customers">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <set name="phones" table="customer_phone" cascade="all" lazy="true"">
           <key column="customer_id" />
           <many-to-many column="phone_id" class="Phone" />
        </set>
    </class>

    <class name="Phone" table="phones">
       <id name="id">
          <generator class="native" />
       </id>
       <property name="number" />
       <property name="type" />
       
      <set name="customers" inverse="true" table="customer_phone">
             <key column="phone_id"/>
             <many-to-many column="customer_id"
                         class="Customer"/>
      </set>   
   </class>
   
</hibernate-mapping>



Unit test
Code:
   public void testFetchPhonesEager() throws Exception
   {
      Session session = sessionFactory.openSession();
      Criteria criteria = session.createCriteria( Customer.class, "customer" )
      .add( Restrictions.idEq( 1l ) )
      .createAlias( "phones", "p")
      .setFetchMode( "p", FetchMode.JOIN );
     
      List<Customer> customers = criteria.list();
      Set<Customer> customerSet = new HashSet<Customer>(customers);
      // uncomment these two lines and everything works fine
//      for (Customer customer : customers)
//         Hibernate.initialize( customer.getPhones() );
      session.close();

      final Customer customer = customers.get( 0 );
      // Problem: LazyInitializationException is thrown!!!
      assertTrue(customer.getPhones().size() == 3);
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 8:53 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Change your criteria to:
Code:
session.createCriteria( Customer.class, "customer" )
      .add( Restrictions.idEq( 1l ) )
      .setFetchMode( "phones", FetchMode.JOIN );

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 9:08 am 
Newbie

Joined: Wed Nov 05, 2008 1:17 pm
Posts: 7
Thanks! That does work. So....looks like using createAlias() without the
jointype parameter will default to an inner join.

Can I do this without using an outer join? Is there a way to use a select or sub select to fetch the phones collection eagerly?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 9:46 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
tdennison wrote:
Thanks! That does work. So....looks like using createAlias() without the
jointype parameter will default to an inner join.

Can I do this without using an outer join? Is there a way to use a select or sub select to fetch the phones collection eagerly?



Code:

session.createCriteria( Customer.class, "customer" )
      .add( Restrictions.idEq( 1l ) );
...
Hibernate.initialize(customer.getPhones());


otherway, you can set fetch mode to subselect in your hbm.xml
Code:
<set name="phones" fetch="subselect"...>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 10:48 am 
Newbie

Joined: Wed Nov 05, 2008 1:17 pm
Posts: 7
I can get the Hibernate.initialize() to work properly.

However, I using the fetch="subselect" doesn't seem to work. Is there
some way to use fetch="subselect" in an eager manner with criteria? I
tried FetchMode.DEFAULT (which is supposed to use the mapped fetch
method) to no avail.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 11:15 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
No, I think its not possible, you can only enable it using join.

_________________
-----------------
Need advanced help? http://www.viada.eu


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