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.  [ 10 posts ] 
Author Message
 Post subject: Problem after session.Refresh()
PostPosted: Mon Nov 06, 2006 4:47 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
I needed to add a session.Refresh() on my entity to check if it was changed before update it, now the check works fine but when i try to update my entity i'm getting the following error :

a different object with the same identifier value was already associated with the session

before i make the refresh all was ok.
in another point of my app, i see that if i call a session.Clear() before call the Update() the error was not generated.

is the right way (the Clear() ) or is there something i didn't understand ?

thanks

Hibernate version: 1.2.0 beta 1

Full stack trace of any exception that occurs:
in NHibernate.Impl.SessionImpl.CheckUniqueness(EntityKey key, Object obj) in C:\Documents and Settings\luke\Documenti\Riferimenti VS2005\NHibernate\NHibernate\Impl\SessionImpl.cs:riga 1998

Name and version of the database you are using: Oracle 10g


Top
 Profile  
 
 Post subject: Re: Problem after session.Refresh()
PostPosted: Mon Nov 06, 2006 9:13 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
berets wrote:
I needed to add a session.Refresh() on my entity to check if it was changed before update it, now the check works fine but when i try to update my entity i'm getting the following error :

a different object with the same identifier value was already associated with the session

before i make the refresh all was ok.
in another point of my app, i see that if i call a session.Clear() before call the Update() the error was not generated.


I think you may want to re-read the NHibernate documentation. I think it's clear that Update() doesn't do what you think it does.

Update() is intended to associate an existing object with a given session telling NHibernate this record will already exist and these values should be updated to the database. It does not mean "Call an UPDATE SQL call." NHibernate relies on the session's tracking which will tell it when a given object is dirty or not. When you call flush and there are dirty objects in the session NHibernate will save all of the changes to those objects.

Since you are calling Refresh() I know that object is already in the session. Secondly since you are calling Refresh() the object will not be dirty until another update is made to that object. If you call Refresh() change a property of that object, and then call Flush() the changes you made to that object will be persisted to the data store.

Hope that helps...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 9:45 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
i know that Update() doesn't run an UPDATE on database, in fact my throuble is that i need to Refresh() an entity to check if another instance of my application (running on another computer) modified it.

then i need to change a property and Update() and Flush() my session.

before i added session.Refresh(entity) all was ok, i load,change and update my entities, now after i Refreshed an entity, when i call the Update() method i'm getting that error.

i read deeper the documentation but without results, the Refresh() method isn't well explained.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 9:49 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
berets wrote:
before i added session.Refresh(entity) all was ok, i load,change and update my entities, now after i Refreshed an entity, when i call the Update() method i'm getting that error.


Ok, I'm still not quite grasping what you are trying to do. I'm trying to tell you not to call the Update() method. There is no need to call the Update() method.

Refresh is basically like Calling Load() for an object except that you already have the object loaded and want the values of that object re-loaded. All you have to do is update your properties after this point and call Flush().

What is the problem with doing that?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 11:58 am 
Newbie

Joined: Thu Nov 02, 2006 8:41 am
Posts: 13
Location: Argentina
at first, you have to load(ObjectToModify), once you have loaded the Object you have to modify, set the objects values whth the setters methods, and then in the same session execute s.update(ObjectToModify)



Transaction tx= null;
tx= s.beginTrasnsaction();

Object oToModify = s.load(Object.class,idFromObject);
o.set(...);

s.update(o);

tx.commit();



is this whath are you looking for?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 12:04 pm 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
before :

Code:
item.AvailabilityItem.BusyAvailability--;
this.m_session.Update(item.AvailabilityItem);
this.m_session.Flush();


all works fine!

after :

Code:
this.m_session.Refresh(item.AvailabilityItem);
item.AvailabilityItem.BusyAvailability--;
this.m_session.Update(item.AvailabilityItem);
this.m_session.Flush();


exception throwed! (on the Update() row.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 4:53 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
i'm trying to do the following things :

i've an entity that in the real world is an availability date, i need to allow user to choose an availability date and then mark it as busy; until today all was ok, but since next week my application will be used by more than user at the same time, so before allow an user to busy an availability date i need to check that displayed one is free again (another user could take it meanwhile)

so i opted for session.Refresh() to get a forced check on the database

the problem is that after i made a Refresh() I cannot call an Update to save my changes.

any help will be appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 6:28 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
found the problem :

if i use an entity incapsulated in another object i'm getting the error.

this code doesn't work

Code:
this.m_session.Refresh(item.AvailabilityItem);
item.AvailabilityItem.BusyAvailability--;
this.m_session.Update(item.AvailabilityItem);
this.m_session.Flush();


this one instead works fine

Code:
Availability avy = this.m_session.Get<Availability>(item.AvailabilityItem);
avy.BusyAvailability--;
this.m_session.Update(avy);
this.m_session.Flush();


even this works

Code:
Availability avy = item.AvailabilityItem;
this.m_session.Refresh(avy);
avy.BusyAvailability--;
this.m_session.Update(avy);
this.m_session.Flush();


is it a bug ?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 9:23 am 
Newbie

Joined: Thu Nov 02, 2006 8:41 am
Posts: 13
Location: Argentina
hibernate needs to load the object in it's session to can update, you can't execute Refresh to an entity is not inn Hibernate Session, thus is that you have to load entity first and ther update it's values, and then update session


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 9:41 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
i know it, but i've 2 considerations:

1) the error is that NH find the entity 2 times and not that cannot found it;
2) i've another piece of code where i retrieve an entity from the Tag property of a treenode, and in that case works fine

Code:
Availability avy = schMain.SelectedAppointments[0].Tag as Availability;
this.m_session.Refresh(avy);
avy.BusyAvailability++;
this.m_session.Update(avy);
this.m_session.Flush();


the difference seems to be that if i assign the incapsulated entity to a new entity and then Refresh(), works; if i refresh directly the incapsulated entity, error.


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