-->
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: eager fetching two one-to-many associations the same time?
PostPosted: Sun Jul 10, 2005 9:06 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
i am using hibernate 3.0.3.

i have a mappinig file for class Business which has two one-to-many associations as follows:

<set name="contacts" cascade="all-delete-orphan">
<key column="BUSINESS_ID"/>
<one-to-many class="Contact"/>
</set>
<set name="addresses" cascade="all-delete-orphan">
<key column="BUSINESS_ID"/>
<one-to-many class="Address"/>
</set>

i use Criterial API for eager fetching Business instances as follows:

Example exampleBusiness = Example.create(example).ignoreCase().enableLike(MatchMode.ANYWHERE);
Criteria crit = getSession().createCriteria(Business.class).add(exampleBusiness);
crit.setFetchMode("contacts", FetchMode.JOIN );
crit.setFetchMode("addresses", FetchMode.JOIN );
return crit.list();

howoever, i always got failed lazy initializastion error as follows:

ERROR LazyInitializationException:19 - failed to lazily initialize a collection (Business.addresses) - no session or session was closed

i looked at the generated SQL statement, there was no outer join with Address. what is more puzzling to me is that in the mapping file for Business, if i move association addresses ahead of contacts as follows:

<set name="addresses" cascade="all-delete-orphan">
<key column="BUSINESS_ID"/>
<one-to-many class="Address"/>
</set>
<set name="contacts" cascade="all-delete-orphan">
<key column="BUSINESS_ID"/>
<one-to-many class="Contact"/>
</set>

then i get lazy initialization error complaining about contacts not being initialized.

could some experts out there shed some light on this? did i do something wrong.

thanks very much!

pete


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 1:19 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
any help is very much appreciated!

regards, pete


Top
 Profile  
 
 Post subject: Re: eager fetching two one-to-many associations the same tim
PostPosted: Fri Dec 30, 2005 4:31 am 
Newbie

Joined: Thu Dec 29, 2005 7:35 am
Posts: 5
hi

i had similar problems with hibernate 3.1 and i fixed it by doing this stupid code:

Code:
            ...
            criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
            criteria.setFetchMode(attr1, FetchMode.JOIN);
            criteria.setFetchMode(attr2, FetchMode.JOIN);
            ...
            List l =new ArrayList();
            ScrollableResults sr = criteria.scroll(ScrollMode.FORWARD_ONLY);
            while (sr.next()) {
                l.add(((Map)sr.get(0)).get("this"));
            }


i think the list() is broken when used with ALIAS_TO_ENTITY_MAP
hope it helps someone!

fabian


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.