-->
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: criteri api: one to many on a join table
PostPosted: Sat Mar 24, 2007 7:22 pm 
Newbie

Joined: Sun Feb 04, 2007 11:58 am
Posts: 6
Hi,

could come nody provid me an example of using the criteria api for a one to many unidirectional relationship with a join table.
I have the USER table that have a one to many unidirectional realtionship with ROLE table (where a user could have many roles)...and i have UserRole join table.

I want to know how i could use the criteria api to retrieve users for a specific role.

could come body give me an example.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 1:55 am 
Beginner
Beginner

Joined: Thu Feb 01, 2007 3:08 am
Posts: 20
Can u paste the mapping ?

You can use the below one .


session.createCriteria(user.class).createCriteria("userrole")
.add(Expression.eq("role", "Search")).list();

here userrole is a collection property in user class containing the data from join table.

Regards
Gokul


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 4:03 am 
Newbie

Joined: Sun Feb 04, 2007 11:58 am
Posts: 6
here is the mapping:

For the member model:
<hibernate-mapping>

<class name="com.motorola.osrb.justifications.client.model.OSRBMember" table="OSRBMEMBERS">
<cache usage="read-write"/>
<id name="ID" column="ID" type="long"><generator class="increment"/></id>
<property name="coreid" column="coreid"/>
<property name="state" column="state"/>

<set name="roles" table="OSRBMEMBERROLE">
<cache usage="read-write"/>
<key column="ID"/>
<many-to-many column="roleID"
unique="true"
class="OSRBRole"/>
</set>
</class>

</hibernate-mapping>
--------------------------------------------------------
And for the Role model:
<hibernate-mapping>

<class name="com.motorola.osrb.justifications.client.model.OSRBRole" table="OSRBROLE">
<cache usage="read-write"/>
<id name="ID" column="roleID" type="long"><generator class="increment"/></id>
<property name="role" column="role"/>
</class>
</hibernate-mapping>


do you think that example code that you post is going to work for this mapping. I fallow up the hibernate tutorial advice which is telling to use a join table for one to many unidirectional association in my mapping xml code.

I need to be able to retrieve all the members for a specific role...and also obtain the role of the specific member...by using the criteria api.

Thanks for your help.


Top
 Profile  
 
 Post subject: try this
PostPosted: Mon Mar 26, 2007 5:17 am 
Beginner
Beginner

Joined: Thu Feb 01, 2007 3:08 am
Posts: 20
the the below one

session.createCriteria(OSRBMember.class).createCriteria("roles")
.add(Expression.eq("ID", "Search")).list();

i hope this would work perfectly !!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 8:55 am 
Newbie

Joined: Sun Feb 04, 2007 11:58 am
Posts: 6
session.createCriteria(OSRBMember.class).createCriteria("roles")
.add(Expression.eq("ID", "Search")).list();

i presume that this must be the code to retreive roles for a specific OSRBMember where ID is the id of the OSRBMember.

How should i do if i want to retrieve all the OSRBMembers for a specific role (Example role=OSSEXpert)


Top
 Profile  
 
 Post subject: try he below query
PostPosted: Mon Mar 26, 2007 11:39 pm 
Beginner
Beginner

Joined: Thu Feb 01, 2007 3:08 am
Posts: 20
session.createCriteria(OSRBMember.class).createCriteria("roles")
.add(Expression.eq("role", "OSSEXpert")).list();

this will retrieve all the users having role OSSEXpert


PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: try he below query
PostPosted: Mon Mar 26, 2007 11:40 pm 
Beginner
Beginner

Joined: Thu Feb 01, 2007 3:08 am
Posts: 20
session.createCriteria(OSRBMember.class).createCriteria("roles")
.add(Expression.eq("role", "OSSEXpert")).list();

this will retrieve all the users having role OSSEXpert


PS : please don't forget to give credits below if you found this answer useful :)


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.