-->
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 deleting
PostPosted: Tue May 29, 2007 4:18 am 
Beginner
Beginner

Joined: Tue May 29, 2007 3:14 am
Posts: 28
I have two classes, User and UserAuthorization. When i try to delete a user, the error "a different object with the same identifier value was already associated with the session: 78, of class: User" occurs. Before deleting the User object I delete all UserAuthorizations of the user. I don't know what I can do to resolve this problem.

Hibernate version:
1.0.4

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="User" table="User" dynamic-update="false" dynamic-insert ="false">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Name" column="Name" type="String" length="50" />
<property name="Password" column="Password" type="String"/>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="UserAuthorization" table="UserAuthorization" dynamic-insert="false" dynamic-update ="false" batch-size="100">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="User" column="ID_User" class="User" fetch="join"/>
<many-to-one name="Function" column="ID_Function" class="Function" fetch="join"/>
<property name="Available" column="Available" type="integer"/>
</class>
</hibernate-mapping>

Full stack trace of any exception that occurs:
bei NHibernate.Impl.SessionImpl.Delete(Object obj) in \NHibernate\src\NHibernate\Impl\SessionImpl.cs:Zeile 1213


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 4:35 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

I think the problem is due to the fact that you're fetching the User and the UserAuthorization objects with 2 different sessions, which means that the regular User and the User that is included in the UserAuthorization ("User"-property) are not one and the same object.
When you pass the regular User object to the session after having passed the UserAuthorizations to the session, you're getting this error, which is normal.

A possible solution is to re-assign the User object:
Code:
user = session.Get<User>(user.ID);


This way, you're asking the session to fetch you the user that is currently present in the session (it's cached and you're using the primary key, so you won't have a roundtrip to the database). When you'll execute the delete, you shouldn't have the error any more.

Code:
session.Delete(user);


There are of course other ways to avoid this error, just ensure that you don't use two objects representing the same database record in one session.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 5:10 am 
Beginner
Beginner

Joined: Tue May 29, 2007 3:14 am
Posts: 28
Thanks for the quick reply. Now it works fine! :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 1:55 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I know it's been solved, but FWIW you can also lock (Session.Lock) your original User object to the new session before loading the UserAuthorization.


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.