-->
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.  [ 4 posts ] 
Author Message
 Post subject: How define this HQL in API criteria ??
PostPosted: Wed Jun 28, 2006 5:14 pm 
Beginner
Beginner

Joined: Thu Jun 01, 2006 11:27 am
Posts: 28
How define this HQL with the API criteria ?? Is it possible??

select ui.Item
from UserItem ui
where ui.User.Id =1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 6:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that all associations are bidirectional, it would be
Code:
Criteria crit = session.createCriteria(Item.class, "item");
crit.createAlias("UserItem", "ui");
crit.add(Restrictions.eq("ui.User", userWithId1);
If any of those associations don't exist, then you'll need to use subqueries. I can work that out too, if you need it.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 8:45 pm 
Beginner
Beginner

Joined: Thu Jun 01, 2006 11:27 am
Posts: 28
The relation are unidirectional:
UserItem --> Item
UserItem --> User

That is because Item and User clases are part of another system (i can't change it).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 9:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hmm. Then you'll need to use a subquery.
Code:
DetachedCriteria uiCrit = DetachedCriteria.forClass(UserItem.class, "ui");
uiCrit.add(Restrictions.eq("ui.User", userWithId1));

Criteria crit = session.createCriteria(Item.class);
crit.add(Subqueries.in("id", uiCrit));
I'm not sure that that's going to work, though: usually ids and properties are treated differently (have a look at Restrictions.idEq), so Subqueries.in mightn't accept "id" as a valid property. If that's the case, you'll have to map UserItem.userId and UserItem.itemId as normal properties, just to get Subqueries.in to work. To map those columns twice (remember, the columns are in the many-to-one associations) use <property name="userId" formula="userId"/>, not column="userId".

That'd be icky, I hope you don't have to do that.

_________________
Code tags are your friend. Know them and use them.


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