-->
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.  [ 2 posts ] 
Author Message
 Post subject: One to one mapping issue with primary key in a new session
PostPosted: Wed Nov 04, 2009 7:22 am 
Newbie

Joined: Mon Oct 12, 2009 6:07 am
Posts: 7
Hi,

I have a one-to-one mapping like below:


User.hbm.xml
<class name="org.hibernate.tutorial.domain.User" table="user_i">
<id name="userId" column="user_id">
<generator class="assigned"/>
</id> <one-to-one name="userDetails" class="org.hibernate.tutorial.domain.UserDetails" cascade="all" />
</class>

<class name="org.hibernate.tutorial.domain.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="org.hibernate.tutorial.domain.User" constrained="true" />
</class>

Below is the code used for saving user object
User user = new User();
user.setUserId("testuser2");
user.setPassword("testpassword");
user.setUserType("S");

UserDetails userDetails = new UserDetails();
userDetails.setUserFirstName("fname");
/user.setUserDetails(userDetails);
userDetails.setUser(user);
for (int i = 0; i < 4; i++) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
}


It works fine for the first time when I load my session, it inserts both user and user_details in the tables.
But once I start a new session and try saving the same user object, it again tries to insert user details object which fails, as there is a already present in the database.
Below is mapping used in .cfg.xml file
<property name="hbm2ddl.auto">update</property>

How to overcome the abopve issue? DO i need to explicitly assign id to user details?Any kind of help will be appreciated here. Thanks


Top
 Profile  
 
 Post subject: Re: One to one mapping issue with primary key in a new session
PostPosted: Wed Nov 04, 2009 7:34 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why do you save it in a loop?? In that case you have to set the ide explicitly.

_________________
-----------------
Need advanced help? http://www.viada.eu


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

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.