-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem inserting/updating when <generator class="as
PostPosted: Mon Jun 14, 2004 2:47 pm 
Newbie

Joined: Wed Jun 09, 2004 2:41 pm
Posts: 14
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.