-->
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: Do I need to make this bidirectional?
PostPosted: Thu Oct 21, 2004 2:15 pm 
Newbie

Joined: Tue Sep 28, 2004 2:03 pm
Posts: 9
Location: Fullerton, CA
I have a many-to-many relationship set up where a User can have many Roles and many Users can have the same Role. Do I need to make the relationship bidirectional to query Users by Role?

For example, given a Role's id, I'd like to get a list of the Users that have that Role. My application is already written, so I'd prefer not to have to go back and make the relationship bidirectional just for this query.

Thanks,
Brian


Hibernate version:
2.1.6

Mapping documents:

User.hbm.xml:
<hibernate-mapping>
<class name="com.briankuhn.vocab.data.value.User" table="users">
<id name="id" column="id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="password" column="password" not-null="true"/>
<set name="roles" table="user_roles" cascade="save-update">
<key column="user_id"/>
<many-to-many class="com.briankuhn.vocab.data.value.Role" column="role_id"/>
</set>
</class>
</hibernate-mapping>

Role.hbm.xml
<hibernate-mapping>
<class name="com.briankuhn.vocab.data.value.Role" table="roles">
<id name="id" column="id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
no code yet

Full stack trace of any exception that occurs:
no exceptions yet

Name and version of the database you are using:
hsqldb vX.X

The generated SQL (show_sql=true):
no sql yet

Debug level Hibernate log excerpt:
no logs yet


Top
 Profile  
 
 Post subject: Was I unclear?
PostPosted: Tue Oct 26, 2004 10:32 pm 
Newbie

Joined: Tue Sep 28, 2004 2:03 pm
Posts: 9
Location: Fullerton, CA
Users belong to many Roles.
Roles contain many Users.

I want to be able to find all Users that belong to Role x.
I want to be able to find all Roles that contain User y.

Will a unidirectional mapping (like the one below) work, or do I need to make the relationship bidirectional?

If I can keep it unidirectional, what would the HQL query look like?

Thanks,
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 2:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can keep it unidirectionnal
(also be carefull with word "user")

"select elements(user.roles) from Uzer user where uzer.id = "

or "select uzer from User uzer, Role role
where role.xxx = yyyy
and role in elements(uzer.roles"

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 3:22 pm 
Newbie

Joined: Wed Sep 29, 2004 10:45 am
Posts: 6
I came cross a similiar situation using Critiera API. How can I make it work with Criteria API?

unidirectional mapping like A(1) ---> B(*), I am trying to query B by giving A. Since there is no association to A in B, how can I construct my Criteria for B?

Thanks,
zeng


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.