Hi,
Have you already tried to put cascade="all" in the two <set> mappings?
Like this:
<set ... cascade="all">
Hope this help!
Fred
[quote="sreten"]Hi, it would be nice if someone help me
[b]Hibernate version:3.1.2[/b]
[b]Mapping documents:[/b]
[i]<class name="cid.beans.UserRole" table="UserRole">
<synchronize table="USERS" />
<synchronize table="ROLES" />
<composite-id name="id" class="cid.beans.ID">
<key-property name="userID" />
<key-property name="roleID" />
</composite-id>
<property name="asigned" type="java.util.Date" />
<many-to-one name="user" column="userID" insert="false"
update="false" />
<many-to-one name="role" column="roleID" insert="false"
update="false" />
</class>
<class name="cid.beans.User" table="USERS" lazy="false">
<id name="id" column="userID">
<generator class="native" />
</id>
<property name="username" />
<property name="password" />
<set name="userRole" inverse="true">
<key column="userID"/>
<one-to-many class="cid.beans.UserRole"/>
</set>
</class>
<class name="cid.beans.Role" table="ROLES">
<id name="id" column="roleID">
<generator class="native" />
</id>
<property name="rolename" />
<property name="shortname" />
<set name="roleUser" inverse="true">
<key column="roleID" />
<one-to-many class="cid.beans.UserRole" />
</set>
</class>[/i]
[b]Code between sessionFactory.openSession() and session.close():[/b]
If i save a user with his roles this way, hibernate doesnt generate insert statement by it self. But, when i load a user from db, it generates the select statements for UserRole and Role. Do i have to explicitly save UserRole entity, or am i doing it wrong?
[i] User user = new User();
user.setUsername("sreten");
user.setPassword("260979");
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
Role role = (Role) session.get(Role.class, 1);
session.save(user);
UserRole ur = new UserRole(user, role);
// session.save(ur);
user.getUserRole().add(ur);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();[/i]
[b]Name and version of the database you are using:hsqldb 1.8.0.7[/b]
Thanks...[/quote]
|