-->
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.  [ 7 posts ] 
Author Message
 Post subject: query question involving a Set property
PostPosted: Fri Dec 16, 2005 11:10 pm 
Newbie

Joined: Tue Dec 13, 2005 12:16 pm
Posts: 6
Hi,

I'd like to write a query that only returns objects where
the elements of a set property meet certain conditions.

This is what I tried but it doesn't seem to be correct

String query = "select profile from Profile as profile " +
"where exists c in elements(profile.criteria) and " +
"c.attribute = 'blue'";

Thanks for any tips.
--
Nick


Hibernate version: 3.1

Mapping documents:

<class name="Profile" table="PROFILES">
<id name="id" column="PROFILE_ID">
<generator class="native"/>
</id>

<property name="name"/>

<set name="scores" table="ProfileScores">
<key column="PROFILE_ID"/>
<many-to-many column="SCORE_ID"
unique="true"
class="Profile$Score"/>
</set>

<set name="criteria" table="ProfileCriteria">
<key column="PROFILE_ID"/>
<many-to-many column="CRITERIA_ID"
unique="true"
class="Profile$Criteria"/>
</set>

</class>


Code between sessionFactory.openSession() and session.close():

String query = "select profile from Profile as profile, Profile$Criteria as criteria " +
"where exists c in elements(profile.criteria) and " +
"c.attribute = 'political'";

//"criteria.attribute = 'diet'";

i = hSession.createQuery(query).list().iterator();

Name and version of the database you are using:

hsqldb


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 12:37 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Try
Code:
select profile
from Profile profile
where profile.criteria.attribute='blue'


Top
 Profile  
 
 Post subject: query question involving a Set property
PostPosted: Mon Dec 19, 2005 10:33 am 
Newbie

Joined: Tue Dec 13, 2005 12:16 pm
Posts: 6
Thanks!

Now what if I'd like to impose a condition on two or more elements of the Set. For instance, the condition that there exists a 'blue' and 'red' attribute in the Set.
--
Nick


Top
 Profile  
 
 Post subject: query question involving a Set property
PostPosted: Mon Dec 19, 2005 12:13 pm 
Newbie

Joined: Tue Dec 13, 2005 12:16 pm
Posts: 6
Actually, what I'm really after is a query that imposes several conditions on an element of the Set. Something like

select profile
from Profile profile
where (profile.criteria.attribute='blue' and
profile.criteria.value = 10)
and
(profile.criteria.attribute='green' and
profile.criteria.value = 5)

In words, selecting Profiles having one criteria element
with attribute='blue', value = 10 and another element with
attribute = 'green' and value = 5. Thanks for any tips.
--
Nick


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 5:17 am 
Newbie

Joined: Tue Jun 19, 2007 7:25 am
Posts: 7
I am facing the same problem now, but unfortunately there is no answer to this posting yet.
Does anyone know a solution to this or might point me to the right direction?

Thanks
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 15, 2007 10:18 am 
Newbie

Joined: Thu May 12, 2005 10:40 am
Posts: 16
element is tricky.
maybe this can help you ;-)

Code:
FROM propfile p
WHERE exists (select pCRIT.value FROM elements(p.criteria) pCRIT
               WHERE pCRIT.attribute='blue' AND pCRIT.value = 10)


*g
Helmut


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 2:50 am 
Newbie

Joined: Fri Jun 30, 2006 2:16 am
Posts: 4
I'm trying to achieve the same thing, but the example given by Helmut doesn't work. "Unexpected token: elements"... As far as I know elements can only be used in the where clause. Is there any other way to get this done?


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