-->
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 how to configure cascade get
PostPosted: Sat Jan 22, 2005 10:05 am 
Newbie

Joined: Sat Jan 22, 2005 9:39 am
Posts: 10
Hi,

I have a user class and a group class
A user class has groups.
A group class has users.
I have no UserGroup class.

I have 3 tables :
T_USER : PK is "id"
T_GROUP : PK is "id"
T_USER_GROUP : PK is composite : userid,group_id"

The data :
A user named 'zizou'
A group named 'Zerezo'.
The user zizou belongs to the group 'Zerezo'

*) question 1 ( mapping ):
Is my mapping ok ? (see the mapping below )
It works but there is maybe a better way.

*) question 2 ( cascade ):

When i get the user, i make 3 sql requests :
- One for getting the user Zizou
- One for getting the user groups => Zerezo
- One for getting the users inside the group Zerezo

What do i have to do if i only want the user without his groups ?
What do i have to do if i want the user with his groups but i don't
want to get the user list for each group selected.

See my code below.

Thanks a lot.
I'm new to hibernate.


Hibernate version:
2.1.7c

Mapping documents:

<hibernate-mapping package="com.a2a" default-cascade="none">

<class name="User" table="T_USER"
dynamic-update="false" dynamic-insert="false"
select-before-update="false">
<id name="id" column="id" type="int" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">SEQ_USER</param>
</generator>
</id>
<property name="login" type="string" not-null="true"/>
<property name="password" type="string" not-null="true" column="passwd"/>
<set name="groups" table="t_user_group" cascade="none">
<key column="user_id"/>
<many-to-many column="group_id" class="Group"/>
</set>
</class>

<class name="Group" table="T_GROUP"
dynamic-update="false" dynamic-insert="false"
select-before-update="false">
<id name="id" column="id" type="int" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">SEQ_GROUP</param>
</generator>
</id>
<property name="name" type="string" not-null="true"/>
<set name="users" table="t_user_group" cascade="none">
<key column="group_id"/>
<many-to-many column="user_id" class="User"/>
</set>
</class>
</hibernate-mapping>

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

Criteria crit = oSession.createCriteria(User.class);
crit.setMaxResults(1);
crit.add(Expression.eq("login", "zizou"));
System.out.println("Recherche");
List listUser = crit.list();
System.out.println("Fin Recherche");
if (listUser != null && listUser.size() > 0)
User oUser = (User) listUser.get(0);

Name and version of the database you are using:

Postgresql 7.3.4

The generated SQL (show_sql=true):

Hibernate: select this.id as id0_, this.login as login0_, this.passwd as passwd0_ from T_USER this where this.login=? limit ?

Hibernate: select groups0_.user_id as user_id__, groups0_.group_id as group_id__, group1_.id as id0_, group1_.name as name0_ from t_user_group groups0_ inner join T_GROUP group1_ on groups0_.group_id=group1_.id where groups0_.user_id=?

Hibernate: select users0_.group_id as group_id__, users0_.user_id as user_id__, user1_.id as id0_, user1_.login as login0_, user1_.passwd as passwd0_ from t_user_group users0_ inner join T_USER user1_ on users0_.user_id=user1_.id where users0_.group_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 22, 2005 10:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read the docs on the "fetch" keyword in HQL and on .setFetchMode() for Criteria Queries.


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.