-->
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: Object with Collection (set) Subquery?
PostPosted: Fri Sep 12, 2003 9:24 am 
Newbie

Joined: Fri Sep 12, 2003 9:13 am
Posts: 10
Hi,

I have an object with various attributes, and it in turn has several set collections mapped. I am trying to query a set of objects out based on both the object parameters and specific members of some collections. To show an example, I have a User and a Preference object defined as such (with extra data stripped for clarity):


Code:
<hibernate-mapping>
     <class name="User" table="users">
     <id column="userId" name="userId" length="64" unsaved-value="null">
          <generator class="uuid.hex"/>
     </id>
     <property name="userName" unique="true" not-null="true" length="64"/>
     <property name="firstName" unique="false" not-null="true" length="128"/>
    <property name="lastName" unique="false" not-null="true" length="128"/>
    <property name="password" unique="false" not-null="true" length="128"/>

     <set name="preferences" lazy="false" inverse="true" cascade="all">
          <key column="userId"/>
          <one-to-many class="Preference"/>
      </set>

     </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
     <class name="Preference" table="user_preferences">
     <id column="preferenceId" name="preferenceId" length="64" unsaved-value="null">
           <generator class="uuid.hex"/>
     </id>
     <property name="name" unique="false" not-null="true" length="64"/>
     <property name="value" unique="false" not-null="true" length="128"/>
     <many-to-one name="user" column="userId" not-null="true"/>
     </class>
</hibernate-mapping>


Now, I would like to do a query along the lines of:

Code:
from user in class User where user.firstName=:firstName and
user.preferences.name['receiveMail']=true


I know this isn't a valid query, but conceptually I would be selecting on both attributes of the User object as well as specific values of specific entries of the Preferences Set. In the example above, I would try using the Preference with the name "receiveMail" and its value would need to be true.

It looks like I need to do this through subqueries, but can anybody give pointers to how I would set this up?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
from User user
    inner join user.preferences pref
where pref.name='receiveMail'
    and pref.value = true


perhaps?


Top
 Profile  
 
 Post subject: Yep!
PostPosted: Fri Sep 12, 2003 11:41 am 
Newbie

Joined: Fri Sep 12, 2003 9:13 am
Posts: 10
That looks like it solved my problem. I actually had to inner join multiple collections, based on user input, but it looks like its working fine.

Thanks for the help!


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.