-->
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.  [ 3 posts ] 
Author Message
 Post subject: NonUniqueObjectException on SaveOrUpdate()
PostPosted: Sat Dec 15, 2007 5:25 am 
Beginner
Beginner

Joined: Tue Sep 11, 2007 5:57 am
Posts: 36
NHibernate version 1.2.0.4

wtih reference to issue logged at http://jira.nhibernate.org/browse/NH-401

Entities : Individual And User

as per the issue at above link the following bug has been fixed:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Configuration cfg = new Configuration();
cfg.AddAssembly("DataAccess");

ISessionFactory factory = cfg.BuildSessionFactory();


ISession sess = factory.OpenSession();

ISession sess2 = factory.OpenSession();

Individual ind = new Individual();

ind.ProfileId = 872; //id from my db ...
ind.Last = "XC"
ind.CreatedBy = ses2.Get<User>(1);

ITransaction trans = sess.BeginTransaction();
sess.SaveOrUpdateCopy(mem);

trans.Commit();
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

here the issue was it threw NonUniqueObjectException exception SaveOrUpdate(), as I used User's object that was loaded using other session object and tried to save Individual object using another session object.
This has been fixed.

But the following one line change to above snippet again throws the same exception.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Configuration cfg = new Configuration();
cfg.AddAssembly("DataAccess");

ISessionFactory factory = cfg.BuildSessionFactory();


ISession sess = factory.OpenSession();
User user = sess.Get<User>(1);

ISession sess2 = factory.OpenSession();

Individual ind = new Individual();

ind.ProfileId = 872; //id from my db ...
ind.Last = "XC"
ind.CreatedBy = ses2.Get<User>(1);

ITransaction trans = sess.BeginTransaction();
sess.SaveOrUpdateCopy(mem);

trans.Commit();
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Any idea why does this happen?
Also as an temporary fix I have commented out CheckUniqueness(EntityKey key, object obj) function call from function DoUpdateMutable and it is working fine. Though I am not sure whether this will affect any other scenario, please advise.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 12:22 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We have a lot of cases where we need to reattach entities fetched in another (closed) session. After enountering so many NonUniqueObjectException problems that couldn't be cleanly avoided, we finally resorted to this approach:

1. Define globally visible current context for the "long" conversation that opens many different sessions and needs to reattach entities from one session to another.

2. Implement IInterceptor.Instantiate.

3. Have your interceptor keep a dictionary of dictionaries where the outer dictionary is keyed by the conversation and the inner dictionary contains WeakReferences keyed by entity class name / ID combination.

4. In IInterceptor.Instantiate, get/create the inner dictionary for the current conversation context, and if the requested entity type/ID is found in there, return that instead of creating a new instance. Check for dead WeakReferences and remove them.

5. Remove the outer dictionary entry for the conversation when the conversation ends (detect this by listening to events).

This approach essentially provides your own "1st level cache" for the entities you are keeping disconnected between sessions, and prevents NonUniqueObjectException. So far we haven't had any problems with it. However, it still may be possible to get NonUniqueObjectException since IInterceptor.Instantiate doesn't get called on a proxy until the proxy is initialized, and if you reattach an uninitialized proxy, a different instance of the same entity type/ID could have already gotten into the session indirectly ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 12:28 am 
Beginner
Beginner

Joined: Tue Sep 11, 2007 5:57 am
Posts: 36
Thanks for the information, I will work a bit on this and let you know.


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