-->
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.  [ 11 posts ] 
Author Message
 Post subject: Update(): new row instead of updated row (object lifecycle?)
PostPosted: Thu Sep 14, 2006 6:30 am 
Newbie

Joined: Fri Feb 24, 2006 7:07 am
Posts: 10
dear community,

i have a very strange problem regarding session's update() function:

at one point of my application, users can select a dataset to be editted which is loaded out of the database (via Criteria-API) and then shown in an edit mask.
Nothing special so far.

Now, i have experienced, that - sometimes - the code behind the "save changes"-button produces a new row in the database (with new PK), instead of having the "orignal" row updated. The code for the button does nothing else than just calling Session.Update().

I cannot reproduce this behaviour all time, but it seems to me it has something to do with the "object lifecycle" (so to say); is that possible ?
I looks like, a new row is created by Update() instead of updating the old if the users are lasting too long in the applications edit mask. I assume, there must be some "timestamps" on queried objects telling hibernate to create a new row (with new PK) instead of using the old row.

I have to mention that i'm closing (and throwing away) the session between loading the object and updating.
As i've read in other threads, it should be no problem to close the session between "Object loading -> user edits object -> save object with new session" - am i right ? Or may be this the problem ?

I'm running on NHibernate 1.0.2.0 & MSSQL2000.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 17, 2006 5:55 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Try to user Session.SaveOrUpdate instead session.update() ,
The idea here is about the classes you're creating !
If you've created a new business class and tried to use session.update() that would create a new row().
So the solution is to load object from the db and then updating .

Hope that this solved the problems ..

Regards,
Jojo

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 7:12 am 
Newbie

Joined: Fri Feb 24, 2006 7:07 am
Posts: 10
hi jojorico,

thank you for the reply.

i will try it... the problem is (as said) i can't reproduce this behaviour all the time; some times it happens, and some it doesn't...

what i don't understand exactly, is:
the instance, i'm trying to update, is not a newly created one; it's loaded out of the DB.
may this be a problem when closing the session-object between these two steps ?



bye


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 8:02 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
FEVUser wrote:
hi jojorico,

thank you for the reply.

i will try it... the problem is (as said) i can't reproduce this behaviour all the time; some times it happens, and some it doesn't...

what i don't understand exactly, is:
the instance, i'm trying to update, is not a newly created one; it's loaded out of the DB.
may this be a problem when closing the session-object between these two steps ?


What are the unsaved-values of Id and version or timestamp elements? Do the corresponding properties have appropriate values before calling update?

BTW, did You know that You do not need to call ISession.Update() if changed object was loaded by the same session?

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 8:09 am 
Newbie

Joined: Fri Feb 24, 2006 7:07 am
Posts: 10
Hi gert,

the block of the PK is this one in the hbm.xml file:
Code:
        <id name="ID" type="int">
            <generator class="identity" />
        </id>


Quote:
...or timestamp elements?

i don't have any timestamp elements in the record itself. i just throwed the word "timestamp" into because that's what i assumed hibernate holds "in the background".

Quote:
Do the corresponding properties have appropriate values before calling update?

they should; i'm just loading the object, e.g. modifying the title of the record (a standard string) and then saving it back to the DB.
(nothing special, i'm assuming)? But between loading and saving i'm closing the session.

no, i didn't know that i don't have to call Update() when the object is loaded by the same session :) thank you
but, in my case i'm closing the session between these two calls


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 8:24 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
FEVUser wrote:
they should; i'm just loading the object, e.g. modifying the title of the record (a standard string) and then saving it back to the DB.
(nothing special, i'm assuming)? But between loading and saving i'm closing the session.

no, i didn't know that i don't have to call Update() when the object is loaded by the same session :) thank you
but, in my case i'm closing the session between these two calls


I'm not sure how closing session affects the behaviour, but propably the Update() call is needed. Diconnecting session should not make the Update needed.

Anyway, my observations about Save/SaveOrUpdate/Update behaviour:

1. Save - in case of new entity, will create new row in database. If id generator is not "assigned", the object will have new primary key.
2. Update - Associates the entity with session to be updated on next flush.
3. SaveOrUpdate - will try to guess if Save or Update is needed, based on the unsaved-value of <version> (or <timestamp>) or <id> element.

So, if You are using SaveOrUpdate anywhere, maybe You should add sensible unsaved-value to mappings? Otherwise I suspect You call "Save" instead of "Update" somewhere.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 12:08 am 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Code:
Dear gert,

I'm only using 1 session, reconnecting and disconnecting, and i'm not using ISession.Update().
Do this affect the performance?
Why do i need to open many Sessions in practice?

In some cases, when i'm working with a N-N Relationship, that relationship is correctly added to the join table but if i tried to add multiple relationships at the same time (ex: add multiple [u]items[/u] for one [u]category[/u]), only the last relationship is saved ... It's being overriden on the same row ? sometimes exception is raised !

And the most important thing is the Lazy Loading which i set to true!
But That isn't working with me, all the database rows are being loaded into my application ...

I'm not using the IInterceptor , does that affect my application !! or maybe it can be ignored .

I've already posted these questions but i'm getting no reply(posted from more than a week)

Regards,
Jojo

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 3:18 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
jojorico wrote:
I've already posted these questions but i'm getting no reply(posted from more than a week)

I replied to two of Your questions... I did not find more un-answered ones. Give a glue if there is something more.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject: Dear Gert
PostPosted: Thu Sep 21, 2006 3:23 pm 
Regular
Regular

Joined: Mon Aug 28, 2006 6:35 am
Posts: 66
Location: Middle East
Dear Gert,

Thanks for the reply, or shall i say thanks for the REPLIES!

It's not the first time you reply on a topic of mine as i remember...
As if you were the only active user in this forum ..

10x a lot ...
I don't know if there is anything i can do for you to thank you ..

Regards,
Jojo

_________________
In Code We Trust


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 11:39 am 
Newbie

Joined: Fri Feb 24, 2006 7:07 am
Posts: 10
Gert,

Quote:
So, if You are using SaveOrUpdate anywhere, maybe You should add sensible unsaved-value to mappings? Otherwise I suspect You call "Save" instead of "Update" somewhere.


currently my construction calls Session.Save() when the application edit masks is loaded with a new "item"; when the users are loading the edit mask with an already existing item, the code just calls Session.Update()

I will now try to swith over to Session.SaveOrUpdate(), but i'm not sure if this will fix my problem.
Regarding this "unsaved-value mapping", i'm not exactly sure what you you want me to set it:
nhibernate_reference.pdf wrote:
unsaved-value="any|none|null|id_value"

which parameter should i try, in your oppinion ? should i just copy&paste this "any|none..." ?

strange to me is the following line from this PDF:
Quote:
The unsaved-value attribute is almost never needed in NHibernate 1.0.


my problem is, that my code will go into beta-production next tuesday, and currently i'm not sure how to handle this issue; as said, most of the time it works out correctly, but sometimes it won't.

thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 24, 2006 1:37 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
FEVUser wrote:
currently my construction calls Session.Save() when the application edit masks is loaded with a new "item"; when the users are loading the edit mask with an already existing item, the code just calls Session.Update()


So, in some cases Your code the edit mask is messing up and erronously thinking that the item is new one. Check out all usage path, including the ones like Create new item, save, edit data for same item, save again... Or create item & save, hit back button edit and save again.

FEVUser wrote:
I will now try to swith over to Session.SaveOrUpdate(), but i'm not sure if this will fix my problem.
Regarding this "unsaved-value mapping", i'm not exactly sure what you you want me to set it:
nhibernate_reference.pdf wrote:
unsaved-value="any|none|null|id_value"

which parameter should i try, in your oppinion ? should i just copy&paste this "any|none..." ?


"|" means one of the possible values, so unsaved-value="any" or unsaved-value="none" or unsaved-value="-1", if You set the Id to be -1 when You cerate new unsaved instance.

Gert

_________________
If a reply helps You, rate it!


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