-->
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.  [ 25 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Criteria eager fetching of collection with alias
PostPosted: Tue Jun 28, 2005 4:02 pm 
Newbie

Joined: Wed May 18, 2005 12:14 pm
Posts: 7
I'm using the Criteria API with Hibernate 3.0.5. I would like to eagerly fetch a collection via setFetchMode, but I also need to add a restriction from the associated table, like so:

Code:
Criteria criteria = session.createCriteria( Relation.class );
criteria.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY );
criteria.setFetchMode( "relationLink", FetchMode.JOIN );
criteria.createAlias( "relationLink", "rl" );
criteria.add( Restrictions.ilike( "rl.personId", personId, MatchMode.START ) );
return criteria.list();


If I use setFetchMode with FetchMode.JOIN alone, the collection is eagerly fetched. But if I combine that with createAlias in order to add the restriction from the associated table, the generated SQL query looks fine but the collection now appears to use lazy initialization.

Is it possible to use setFetchMode together with createAlias to perform an eager fetch? If not, is there any alternative to solving this other than using HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 3:27 pm 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
I'm having the same problem... does anybody now how to handle it? Rarlam, did you find any solution?

Tks,
Cristiane


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 10:07 am 
Newbie

Joined: Wed May 18, 2005 12:14 pm
Posts: 7
cfogaca wrote:
I'm having the same problem... does anybody now how to handle it? Rarlam, did you find any solution?


No, I didn't find a good solution. I worked around the problem for now by triggering the lazy initialization in my DAO (while the session was still open). Of course, that's not what I really want because it leads to many more SQL queries.

Hibernate gurus ... am I missing something? Or is this a bug or limitation in the Criteria API?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 10:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is correct behavior and will certainly not be changed.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 11:19 am 
Newbie

Joined: Wed May 18, 2005 12:14 pm
Posts: 7
gavin wrote:
This is correct behavior and will certainly not be changed.


It's not clear to me why this is considered "correct behavior." Let me try rephrasing the question...

Using a Criteria query, I would like to eagerly fetch a collection while adding a restriction from the associated table. How can this be done?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 11:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Um. I thought my post was pretty explicit....

It cannot be done and it should not be done and you are trying to do a Bad Thing.

Instead, you should be using setResultTransformer(ALIAS_TO_ENTITY_MAP).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 12:45 pm 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
Can you post an example with this setResultTransformer(ALIAS_TO_ENTITY_MAP).?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 11:23 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Hi,
I think Gavin suggested to use the following line:
Code:
criteria.setResultTransformer( Criteria.ALIAS_TO_ENTITY_MAP );


However, I found a workaround which in my opinion works even better:
Instead of
Code:
crit.createAlias("drddealunits", "dealunit");         
crit.add(Expression.eq("dealunit.unitId",unitId));


I used:
Code:
crit.createCriteria("drddealunits")
   .add(Expression.eq("unitId", unitId));


(see Hibernate in Action, page 264)
This approach eliminates the need to use an alias, and now eager fetching works well.

Gavin: is what I do also a Bad Thing? If so, can you point me to some documentation on why this is bad?

regards
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 6:29 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Hi,
after some thinking, I think I now know why Gavin says that this is a bad thing:

If you have a 1:n relation between tables A and B, and you add a restriction to B and want to fetch A and B it eagerly, the question would be what happens when you want to navigate from A to B.

Should you only see the data in B that matches the restriction, or should you see all Bs that are related to A?

To my mind, this wouldn't be clear, so it's better not to eagerly fetch the values from B.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 1:30 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:20 am
Posts: 40
Location: Vienna
Hi,

a solution that finally worked fine for me was to set the FetchMode to "subselect" in the mapping file, and lazy to false:

Code:
    <set
        name="drddealunits"
        lazy="false"
        fetch="join"
        inverse="true"
        cascade="none"
    >


This way, only 2 selects are executed: the first to get the "1" side of the 1:n relation, and then a second select with subselects to get the "n" side data.

Note that I only managed to do this in the mapping file, there is no setFetchMode(FetchMode.SUBSELECT).


Top
 Profile  
 
 Post subject: Can not fetch result with detachedCriteria+ResultTransformer
PostPosted: Fri Oct 28, 2005 4:42 am 
Newbie

Joined: Sat Oct 01, 2005 8:22 pm
Posts: 4
I worked all the day, but still can not resolve the following problem:
DetachedCriteria with setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP) give me the list of result, and I can read the Map, but I can not fetch the correct Object itCatprd . Why ? Thanks.

Here is the key codes:

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ItCatprdparent.class,"parent")
.add(Restrictions.eq("parentCatPrdNid", new Integer(2)))
.createAlias("itCatprd","itCatprd")
.add(Restrictions.eqProperty("parent.itCatprd.id", "itCatprd.id"))
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

Criteria criteria = detachedCriteria.getExecutableCriteria(sf.openSession());
List list = criteria.list();

Iterator iter = list.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
Set keys = map.keySet();
Iterator iter2 = keys.iterator();
while ( iter.hasNext() ) {
String key = (String) iter2.next();
// The key is correct
System.out.println("key=" + key);
}
// But itCatprdparent and itCatprd are null ! WHY ?
ItCatprdparent itCatprdparent = (ItCatprdparent) map.get("parent");
ItCatprd itCatprd = (ItCatprd) map.get("itCatprd");
}

And these is the mapping xml code:
(1)
<class name="hs.ItCatprd" table="it_catprd">
<id name="id" type="integer">
<column name="catPrdNId" />
<generator class="native" />
</id>
<many-to-one name="itCompany" class="hs.ItCompany">
<column name="definedByCompanyNId" not-null="true" />
</many-to-one>
<property name="catPrdName" type="string">
<column name="catPrdName" length="100" />
</property>
<set name="itCatprdparents" inverse="true">
<key>
<column name="catPrdNId" />
</key>
<one-to-many class="hs.ItCatprdparent" />
</set>
</class>
(2)
<class name="hs.ItCatprdparent" table="it_catprdparent">
<id name="id" type="integer">
<column name="catPrdParentNId" />
<generator class="native" />
</id>
<many-to-one name="itCatprd" class="hs.ItCatprd">
<column name="catPrdNId" />
</many-to-one>
<property name="sequence" type="integer">
<column name="sequence" />
</property>
<property name="parentNodeGid" type="integer">
<column name="parentNodeGId" />
</property>
<property name="parentCatPrdNid" type="integer">
<column name="parentCatPrdNId" />
</property>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:31 pm 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
If we want to return both the matching Items and Bids, we must ask Hibernate to return each row of results as a Map:

Here is an example:

Iterator itemBidMaps = session.createCriteria(Item.class)
.createAlias("bids", "bid")
.add( Expression.like("this.description", "%gc%") )
.add( Expression.gt("bid.amount", new BigDecimal("100") ) )
.returnMaps()
.list().iterator();

while ( itemBidMaps.hasNext() ) {
Map map = (Map) itemBidMaps.next();
Item item = (Item) map.get("this");
Bid bid = (Bid) map.get("bid");
// Do something
}

(Hibernate in Action page 265)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 10:13 am 
Newbie

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

in my case i can't do it with a fix fetch mode - i really need to set it in the criteria.

i think that the library is broken here because it lets me set the the fetch mode:

Code:
criteria.setFetchMode("whatever", FetchMode.JOIN);


but it doesn't work - or as some one from the hibernate team said "must not work".

but as others wrote it does work when it's fix coded in the mapping...!

i call this a bug... and don't really like to work around it...

fabian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 10:20 am 
Newbie

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

in my case i can't do it with a fix fetch mode - i really need to set it in the criteria.

i think that the library is broken here because it lets me set the the fetch mode:

Code:
criteria.setFetchMode("whatever", FetchMode.JOIN);


but it doesn't work - or as some one from the hibernate team said "must not work".

but as others wrote it does work when it's fix coded in the mapping...!

i call this a bug... and don't really like to work around it...

fabian


Top
 Profile  
 
 Post subject: Re: Criteria eager fetching of collection with alias
PostPosted: Thu Mar 04, 2010 2:55 am 
Newbie

Joined: Wed Feb 07, 2007 8:26 pm
Posts: 17
Did anyone find a solution for this?


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

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.