-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many-to-many and queries
PostPosted: Thu Jun 09, 2005 7:09 am 
Newbie

Joined: Fri May 20, 2005 6:05 am
Posts: 8
I know this has to be documented somewhere but I can not find it.

I have a many to many (users to groups) and want to find all the users by what group they are in.

Hibernate version: 3.0.5

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="UserBean" table="USER">
<id name="id" type="long">
<column name="USER_ID" not-null="true"/>
<generator class="sequence">
<param name="sequence">USER_SEQ</param>
</generator>
</id>
...
<set name="groups" lazy="true" table="USER_GROUP">
<key column="USER_ID"/>
<many-to-many column="GROUP_ID" class="GroupBean"/>
</set>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="GroupBean" table="GROUP">
<id name="id" type="long">
<column name="GROUP_ID" not-null="true"/>
<generator class="sequence">
<param name="sequence">GROUP_SEQ</param>
</generator>
</id>
...
</class>
</hibernate-mapping>

db scripts

CREATE TABLE USER
(
USER_ID NUMBER(12) NOT NULL,
...
CONSTRAINT USER_PK PRIMARY KEY (USER_ID),
)

CREATE TABLE GROUP
(
GROUP_ID NUMBER(12) NOT NULL,
...
CONSTRAINT GROUP_PK PRIMARY KEY (GROUP_ID),
)

CREATE TABLE USER_GROUP
(
USER_ID NUMBER(12) NOT NULL,
GROUP_ID NUMBER(12) NOT NULL,
CONSTRAINT USER_GROUP_PK PRIMARY KEY (USER_ID, GROUP_ID),
CONSTRAINT USER_FK FOREIGN KEY (USER_ID)
REFERENCES USER(USER_ID),
CONSTRAINT GROUP_FK FOREIGN KEY (GROUP_ID)
REFERENCES GROUP(GROUP_ID)
)

POJOs

public class UserBean {

private Set groups;
private long id;
// getter and setter exist
}

public class GroupBean {
private String id;
// getter and setter exist
}

Note my groupbean does not have a set of users because I am worried about circular fetches (I get a user, that uses has a group that group has users those users have groups) if I need the group to have a set of users and have the many-to-many reflected on both sides then how do I avoid the circular fetching?

thanks


Top
 Profile  
 
 Post subject: Re: Many-to-many and queries
PostPosted: Thu Jun 09, 2005 10:28 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
jtrollin wrote:
I know this has to be documented somewhere but I can not find it.

I have a many to many (users to groups) and want to find all the users by what group they are in.
...
...

Note my groupbean does not have a set of users because I am worried about circular fetches (I get a user, that uses has a group that group has users those users have groups) if I need the group to have a set of users and have the many-to-many reflected on both sides then how do I avoid the circular fetching?

thanks


This is really pretty simple using the Criteria interface:

(Assume groupId contains the id of the group you are searching for.)

Code:
List users = session.createCriteria(UserBean.class)
                    .createAlias("groups","group")
                    .add(Restrictions.eq("group.id" ,groupId)
                    .list();


BTW: You don't need to worry about circular fetching. Hibernate takes care of this for you.


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