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=?
|