aspose file tools*
The moose likes Object Relational Mapping and the fly likes Foreign key table inserting duplicate on update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Foreign key table inserting duplicate on update" Watch "Foreign key table inserting duplicate on update" New topic
Author

Foreign key table inserting duplicate on update

Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
hi
i have user and user_detail table very simple with one to one mapping. the issue is its saving fine but when i update it inserts a new row only in the user_details table.
user table is of
user_id
password

user_details is of
userid references user(user_id),
user_first_name .....

here is my mapping file. the isue is any update to user tabel are fine but when i try to update the first name it insert a duplicate row in teh user_Details table. let me stress one thing here i get the user object first closes the session. then again after getting the user input i create the user object and run and do an update on that. i read in some post the same session should be used for fetching and updating which doesn't make sense since you first fetch data present to the user get his input manipulte teh object layer and run an upate on that. any help
<hibernate-mapping auto-import="true" default-lazy="false">

<class name="com.hms.model.User" table="user">
<id name="userId" column="user_id">
<generator class="assigned"/>
</id>
<property name="password">
<column name="password" />
</property>
<property name="userType">
<column name="user_type_id" />
</property>
<one-to-one name="userDetails" class="com.hms.model.UserDetails" cascade="all" constrained="true" />
</class>



<class name="com.hms.model.UserDetails" table="user_details">

<id name="userId" column="user_id">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="com.hms.model.User" constrained="true" />
<property name="userFirstName">
<column name="user_first_name" />
</property>
<property name="userMiddleName">
<column name="user_middle_name" />
</property>
<property name="userLastName">
<column name="user_last_name" />
</property>
<property name="userWorkPhone">
<column name="user_work_phone" />
</property>
<property name="userHomePhone">
<column name="user_home_phone" />
</property>
<property name="userMobilePhone">
<column name="user_mobile_phone" />
</property>

<property name="userDob">
<column name="user_dob" />
</property>
<property name="userEmail">
<column name="user_email" />
</property>
</class>

</hibernate-mapping>
my java code is very simple i open the sesion and do a
session.saveOrUpdate(user)
Thanks
Rashid
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Foreign key table inserting duplicate on update