-->
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.  [ 4 posts ] 
Author Message
 Post subject: NonUniqueObjectException when migrating Oracle to SQLServer
PostPosted: Wed Nov 08, 2006 1:47 pm 
Newbie

Joined: Wed Oct 12, 2005 11:16 am
Posts: 13
Issue:

I am migrating hibernate+spring+tomcat application from oracle10g to sqlserver2005. The application is working fine in oracle. I changed the mapping files i.e, id generation from sequence to identity. It works fine except in a case where a unsaved parent(having null id) has two child records(having null id). In this case while saving, hibernate throws

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 0, of class: com.domain.UserPhone; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.domain.UserPhone

I have gone through various posts on the forum and I tried following scenarios:
1. tried changing unsaved-value
2. saveOrUpdate vs. saveOrUpdateCopy

One thing I noticed is hibernate executes two selects for getting sequence value for userphone (since there are two userphones) but only one insert statement for userphone. In sqlserver, there are no select for fetching the identity values and hibernate is executing two insert for the two userphones. Is this an issue with hibernate version? Should I move to hibernate3?

Please let me know what the issue is as I am trying to figure out since a couple of days.

Hibernate version: 2.1
Name and version of the database :SQLServer2005

userphone.hbm.xml :

<id
name="userPhoneId"
type="java.lang.Long"
column="USER_PHONE_ID" unsaved-value="null"
>
<generator class="identity"/>
</id>
<many-to-one
name="user"
class="com.domain.User"
not-null="true"
>
<column name="USER_ID" />
</many-to-one>

user.hbm.xml:
<id
name="userId"
type="java.lang.Long"
column="USER_ID" unsaved-value="null"
>
<generator class="identity"/>
</id>
<set
name="userhones"
lazy="true"
cascade="all"
inverse="true"
order-by="SORT_ORDER"
>
<key>
<column name="USER_ID" />
</key>
<one-to-many
class="com.domain.UserPhone"
/>
</set>

Code between sessionFactory.openSession() and session.close():

getHibernateTemplate().saveOrUpdateCopy(user);

Full stack trace of any exception that occurs:
org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 0, of class: com.domain.UserPhone; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.domain.UserPhone
org.springframework.orm.hibernate.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:609)
org.springframework.orm.hibernate.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:353)
org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:369)
org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdateCopy(HibernateTemplate.java:611)
com.dao.hibernate.UserDaoHibernateImp.save(UserDaoHibernateImp.java:190)

Thanks in advance

Sri



[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:19 pm 
Newbie

Joined: Tue Sep 19, 2006 4:13 am
Posts: 18
So the issue is as follows:

Quote:
saveOrUpdateCopy
method will try to reassosiate an object to a session, since the unsaved-value="null" in your case and the new object assosiated is also having null as the id , threfore for Hibernate these to are the same entities with same ids.

The apis say for saveOrUpdateCopy :

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved or does not exist in the database, save it and return it as a newly persistent instance. Otherwise, the given instance does not become associated with the session
(Check the mark in bold)

Net Net: Change either of the two unsaved-value or the default ids for uor domain models.

By the way saveOrUpdateCopy is deprecated in latest version better use merge() if you think to take up v3.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:40 pm 
Newbie

Joined: Wed Oct 12, 2005 11:16 am
Posts: 13
Thanks monu for your reponse. I have already tried all possible scenarios like using saveOrUpdate instead of saveOrUpdateCopy, changing unsaved-value to 1 or 0. None of them work. I think the issue is something else.

Thanks

Sri


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 11:07 am 
Newbie

Joined: Wed Oct 12, 2005 11:16 am
Posts: 13
Hi

Can anyone throw some more light on this.

Thanks

Sri


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.