-->
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: Criteria query: How to match inside a Set ?
PostPosted: Thu Oct 12, 2006 1:36 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
I have looked at the documentation, but I could not find a similiar example.....

I have a User class and a UserValue class.

User.java

public Set<UserValue> getUserValues();


Now I want to find all users who have a specific UserValue inside their UserValues.

Criteria criteria = _session.createCriteria(User.class);
criteria.add(Restrictions.eq("userValues",someValue)); ???
criteria.setMaxResults(30);
List<User> users = criteria.list();

Currently I am using:

Query query = _session.createQuery("select user from User user,"
+ " UserValue userValue"
+ " inner join userValue.configValue configValue "
+ " where configValue.valueID="
+ configValue.getValueID().toString()";
query.setMaxResults(30);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 1:45 pm 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
Maybe this is might help:

List users = session.createCriteria(User.class).createAlias("userValue", "userValue").add(Restrictions.eqProperty("userValue.valueID, configValue.getValueID().toString()).list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 3:37 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
Thanks! I got a:

"could not resolve property: userValue of: User"

...because there is not property getUserValue

only a Set<UserValue> getUserValues() method.

:-(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 3:57 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
just checked the docs again....i wish there were more examples forcriteria queries....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 7:05 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
There are two possibilities:

Code:
select distinct parent
from Parent parent
join parent.children child
where child.name = :name

Code:
from Parent parent where :childId in elements(parent.children)


The first query uses a table join; the second uses a subquery. The first query allows constraints to be applied to element properties; the second does not.

You may not need the distinct keyword in the first query.

From FAQ

Pls rate if this helped u


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 9:01 pm 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
Ok My bad
List users = session.createCriteria(User.class).createAlias("userValue", "userValue").add(Restrictions.eqProperty("userValue.valueID, configValue.getValueID().toString()).list();


Change userValue to userValues as your Set within User.class is defined as userValues.

List users = session.createCriteria(User.class).createAlias("userValues", "userValues").add(Restrictions.eqProperty("userValues.valueID", configValue.getValueID().toString()).list();


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.