I
I have a the following one-to-many relation ship.
User-----------------(1...*) --------->UserRole with inverse="false" and <generator class="assigned">
When generator is assigned, which one to use for unsaved-value?
if I use unsaved-value="any" it tries to always insert so I get an exception when it trys to insert a record in UserRole instead of updating.
(Constraint violation) whenever I call session.update(user).
I am able to save User and UserRole with unsaved-value="any" by calling session.create(user)
I can't use unsaved-value="none" as I will be inserting and updating. As I understand none always treats as update.
If I use unsaved-value="id_value" it only creates User record and try to update UserRole record and obviously there will be no record in UserRole when I call session.create(user)
//User.hdb.xml
<hibernate-mapping package="com.beans">
<class name="User" table="USERS">
<id name="userId" column="USERNAME">
<generator class="assigned" />
</id>
<property name="password" column="USERPASS"/>
<property name="repId" column="SALESREPID"/>
<bag name="userroles" cascade="all">
<key column="USERNAME"/>
<one-to-many class="UserRole"/>
</bag>
</class>
</hibernate-mapping>
//UserRole.hbm.xml
<hibernate-mapping package="com.beans">
<class name="UserRole" table="USER_ROLES">
<id name="userId" column="USERNAME" unsaved-value="any">
<generator class="assigned"/>
</id>
<property name="roleName" column="ROLENAME"/>
</class>
</hibernate-mapping>
Here are the POJOs:
public final class User implements Serializable {
private String userId;
private String password;
private String repId;
private List userroles;
....//and getters and setters
}
public final class UserRole implements Serializable {
private String userId;
private String roleName;
.......//and getters and setters
}
Do we need to update separately as shown below?
session.update(user)
session.update(userRole)
session.flush()
session.close()
Since I am evaluating using Hibernate for one of our project, any help will be much appreciated.
Thanks,
Jeelani
|