Hi,
After hours of search without finding I think I can explain my problem here:
It's a very classic relation many to many involving table user , role et user_role. I can persist user and role but not the bidirectionnal relation.
If you could show me what I'm missing :
Hibernate version: hibernate-3.2.0.cr4
Mapping for User :
Code:
......
<class name="verlina.domain.model.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name = "login" />
<property name = "password" />
<set name="roles" table="user_role" cascade="all" lazy="false" outer-join="true">
<key column="id_user"/>
<many-to-many class="verlina.domain.model.Role" column="id_role" />
</set>
</class>
......
Mapping for Role : Code:
....
<class name="verlina.domain.model.Role" table="role">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="users" table="user_role" cascade="all" lazy="false" outer-join="true" inverse="true">
<key column="id_role"/>
<many-to-many class="verlina.domain.model.User" column="id_user" />
</set>
</class>
.....
Code between sessionFactory.openSession() and session.close(): Code:
User u = new User();
u.setLogin("mimi"); u.setPassword("cracra");
Role r = new Role();
r.setName("ROLE_SUPERADMIN5");
u.addRole(r);
session.save(u);
Code that add a role to a user: Code:
class User{
.....
public void addRole(Role r){
getRoles().add(r);
r.getUsers().add(this);
}
.....
}
Probleme:
No warning or error message , the role and user object are correctly persited in the database but not their relation, ie the table user_role is not changed at all.
Any help would be greatly appreciated.