-->
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.  [ 5 posts ] 
Author Message
 Post subject: why session.save(PO) saves all changed objects in database ?
PostPosted: Fri Nov 24, 2006 11:10 am 
Newbie

Joined: Fri Nov 24, 2006 10:43 am
Posts: 3
Location: NOIDA
Using Hibernate trasaction API

I loaded two persistent objects in hibrenate session and I changed properties for both objects. Now even If I only save one of them object using
session.save(object1) , this upades both objects in database.

Why hibernate saves every changed object in database at every flush? Why do I need a method like session.save(po) instead of just session.update();

_________________
Rajneesh
Email : rajneeshk@contata.co.in
-------------------------------------

PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 2:06 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Read the documentation.
Or the new book on JPA / Hibernate.
Or even the old Hibernate in Action would do.
To get the basic idea how Hibernate / ORM works.


Top
 Profile  
 
 Post subject: why session.save(PO) saves all changed objects in database ?
PostPosted: Tue Dec 26, 2006 10:25 am 
Newbie

Joined: Fri Nov 24, 2006 10:43 am
Posts: 3
Location: NOIDA
alesj wrote:
Read the documentation.
Or the new book on JPA / Hibernate.
Or even the old Hibernate in Action would do.
To get the basic idea how Hibernate / ORM works.



Let me explain problem again...

I just want to confirm , why hibernate perform perfom dirty check operation every time even I use session.save(po) , please note that this operation is specific to particular object.

Hibernate should check complete session If I dont specify any PO to save like session.flush(). What is being happened right now , its causing all objects saved in database those have been changed in hibernate session but I dont want to save those.

I know I have to reload same objects again in same session if I dont want to save those changes . But is that really efficient or an overhead ?

_________________
Rajneesh
Email : rajneeshk@contata.co.in
-------------------------------------

PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: Re: why session.save(PO) saves all changed objects in databa
PostPosted: Tue Dec 26, 2006 11:37 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
rajneesh wrote:
Why hibernate saves every changed object in database at every flush? Why do I need a method like session.save(po) instead of just session.update();

You don't. Try calling no save() at all, you'll see updates happen anyway.

As said, you have to read some documentation about how Hibernate manages dirty checking. When retrieving an object with the session, you're not really using your POJO, but instead something that's called a dynamic proxy, which is actually a class that inherits your POJO and was generated at runtime (using cglib library for example).

If you don't want these automatic updates to happen, you should put your object in what's called a "detached" state. To achieve this, you can call session.evict(yourInstance) to prevent updates to happen if you don't call save() on it.

To sum up, by default, all instances retrieved from the session will behave this way.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: Re: why session.save(PO) saves all changed objects in databa
PostPosted: Tue Dec 26, 2006 11:52 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
rajneesh wrote:
Hibernate should check complete session If I dont specify any PO to save like session.flush(). What is being happened right now , its causing all objects saved in database those have been changed in hibernate session but I dont want to save those.

So, call session.evict() on the objects you don't want to be synced (or even session.clear() if you want NO object to be automatically dirty checked).

rajneesh wrote:
I know I have to reload same objects again in same session if I dont want to save those changes . But is that really efficient or an overhead ?

What do you mean? If you call refresh, Hibernate will issue the simplest possible select to retrieve the data. So I don't why it could be considered as an overhead, compared to what you would need to do to "undo" something you did on instances in memory...

But I don't see very well the point of modifying a persistent instance without wanting to persist it?!? Maybe if you try and explain your needs, it will be simpler to help you do what you want :).

Cheers

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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