-->
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.  [ 14 posts ] 
Author Message
 Post subject: Question on Update() method
PostPosted: Mon Dec 08, 2008 9:03 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi,

Hibernate is issuing update statement when called update() even the object is not modified.

ex:

Code:
TestObject testObject = session.get(TestObject.class,"20");

i got the object
without modifying or touching it i am calling update on it.

Code:
session.update(testObject)


hibernate is issuing update statement and incrementing the version number of the object. I got confused what is the scope of dirty checking in hibernate, will dirty check compare only the version without considering whether the object has been changed or not?

Please provide clarification on this.

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 9:37 am 
Newbie

Joined: Mon Sep 29, 2008 5:00 am
Posts: 4
The update() method is for reattaching detached objects, in your mind you could rename it to reattach(). :) This method will always schedule an update, regardless of the object being modified or not.

If you don't want to force an update statement, use the lock() method instead, as in
anotherSession.lock(testObject, LockMode.NONE);

LockMode.NONE tells Hibernate to not perform a version check. With the LockModes READ or UPGRADE there would be a select statement to perform a version check.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 1:16 pm 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Thanks Haf for the quick reply,

My requirement is to update the object if it got modified otherwise it should not issue update statement.

Can you please suggest me what method of hibernate i can use it in this scenario.


Thanks in advance

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 3:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to make sure modifications are only done while the object lives in a session. If you modify a detached object you will put aside Hibernate's default dirty checking mechanism. Basically you need to do what Haf said. For example something like this.

Code:
Object someObject = .... // Detached UNMODIFIED instance
Session session = ... // Get new session
session.beginTransaction();

// Reattach the object before modifying it
// Modifications before this line are not catched by Hibernate!
session.lock(someObject, LockMode.NONE); 

// insert code that modifies someObject....

session.getTransaction().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 12:52 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Thanks nordborg,

I understood the things with your example. What would be the better solution when we are working for web applications where we will get the object through hibernate get() method and send it to UI, where user may or may not modify(user can click on save without even modifying object) at UI and send back for persisting the changes.

In this case, how to keep track whether the object has been modified by the user or not.

isn't hibernate responsible for keeping track?

Thanks in advance

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 3:42 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Isn't hibernate responsible for keeping track?


Yes, Hibernate does that but your objects must of course live in a Hibernate session or else Hibernate can't possibly know what you are changing in your objects.

I am also working on a web project (as I think a lot of Hibernate users are) and we are using the approach outlined in my previous post.

You may also try using Session.merge() which is more forgiving with respect to if the object is already attach to the session or not.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 11:40 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Thanks nordborg for the suggestion,

I would like to know more regarding this...

Code:

Object someObject = .... // Detached UNMODIFIED instance
Session session = ... // Get new session
session.beginTransaction();

// Reattach the object before modifying it
// Modifications before this line are not catched by Hibernate!
session.lock(someObject, LockMode.NONE);

// insert code that modifies someObject....

session.getTransaction().commit();



When the object reached UI it will get detached from the session. Modifications will be done at the UI side and will come to the persistence layer with modified stuff, how we will insert the code that modifies the someObject after reattaching to the session, since it is already modified at UI layer.

Can you please give me the clarification.

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 12:25 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What do you mean with the UI side? Shouldn't the UI side be concerned with presentation only?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 8:53 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
yes UI means Presentation layer only.

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 9:20 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
yes UI means Presentation layer only.


But in a previous post you said that your UI is also updating the objects (...since it is already modified at UI layer...).

If you want Hibernate to keep track of changes you should not call Session.update() since that more or less forces an update with the current state. Use Session.lock() or maybe Session.merge(). Design your application so that you don't have to modify objects that are disconnected from a session. Updates to objects should not happen in the UI layer.

The Model-View-Controller (MVC) pattern is a very popular design pattern in web applications. Look for example at Spring (http://www.springframework.org/) for a commonly used framework for implementing this pattern. It is used by a lot of Hibernate users. Unfortunately that doesn't include myself, so I can't really give any more tips about spring.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 2:52 pm 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Thanks nordborg, your replies are creating interest in me to know more. We are developing our application using MVC pattern and Spring, let me be more clear on this.

We are using POJO as MOdel object and we are also using OpenInSessionViewFilter so that hibernate session will not get close until the UI renders. Spring will do session and transaction management.

Each service method is a transaction when the service method returns to Controller and UI renders then our hibernate session will get close, that means our POJO(or domain object or model) will get disconnected from the session. User will do the neccessary changes that he want in the UI and initiates save action.

We are carrying the same POJO(model object) to service layer from there we are trying to persist the changes that means what ever the changes made to the object will be done is on detached object only. how is it possible that (...Updates to objects should not happen in the UI layer....) .

_________________
Vinay N


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 4:46 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The description you are giving about Spring and MVC seems to be a very good start. But I am pretty sure that there are solutions in Spring that lets you reconnect an object to a new session before updating it. Unfortunately I don't have any first-hand experience with Spring so I can't give any advice on how to do it. I know that there are a lot of other users on this forum that successfully use Spring with Hibernate. Do anyone of you Spring experts have any advice?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 5:15 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi nordborg,

In the hibernate reference manual i've seen following property which can be set at class.

Code:
dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.


even after setting the property to true, still update is behaving same.

can you please share me your thoughts.

_________________
Vinay N


Top
 Profile  
 
 Post subject: Re: Question on Update() method
PostPosted: Fri Dec 14, 2012 4:40 pm 
Newbie

Joined: Sat Nov 06, 2010 4:21 pm
Posts: 3
The dynamic-update property doesn't control whether or not the update is done, it only controls *how* it is done when it is done. It means Hibernate will use an UPDATE statement that only updates the columns that have changed (according to Hibernates dirty checking--which as stated above will be bypassed if update() is used, and Hibernate will act as if all columns were changed). Otherwise, Hibernate will use a previously-prepared UPDATE statement that sends values for *all* updatable columns whether they were changed or not. The downside of dynamic-update is that it must construct a new UPDATE statement every time.


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