-->
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: problem using dynamic-update="true" with versionin
PostPosted: Sun Apr 11, 2004 6:02 am 
Newbie

Joined: Sun Apr 11, 2004 2:02 am
Posts: 1
Location: Melbourne, Australia
Hi,

I've read the hibernate docs and from what i understand if i the set the tag dynamic-update = true, then it should only generate a SQL statement that updates the actual attributes that have changed. I'm also using optimistic locking (version number) and even if i don't update any attributes on an object and call the session.update() method as listed below, the version number is incremented by 1 and the object is updated.

I thought that he desired result would be that Hibernate would check that the object has not changed and would not issue an update statement.
If i had a collection of objects you would expected only the objects that had changed, would be updated and the unchanged objects would not for performance. I also turned on the property show_sql=true so i see what sql statements that were being issued by Hibernate

Hibernate: update Drp set version=? account_id=?, type=? where drp_id=? and version=?

If it Hibernate was just updating the version, i would have expected that as i had set dynamic-update = true that the log would have shown the following instead.

Hibernate: update Drp set version=? where drp_id=? and version=?

I guess i'm unsure if i am doing something wrong or if Hibernate doesn't support this type of functionality that i am describing. I would greatly appreciate some help. List below SW, code and mapping docs.

Regards
Mark


Anyhow, i using
Hibernate 2.1.2
mysql 4.0.12 win
WSAD 5.1.1

Update code.
--------------
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.update(myObject);
session.flush();
tx.commit();


Mapping Documents
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.thenational.model.Drp" table="Drp" dynamic-update="true" >

<id name="drpId" type="long" unsaved-value="null" column="drp_id">
<generator class="identity"/>
</id>

<!-- Use a version number for optimistic locking -->
<version name="version"
column="version"
type="integer"/>

<property name="accountId" column="account_id" />

<property name="type" not-null="true"/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 8:59 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
Did you find an answer to your problem? I also have the same problem.
Gavin told me the following in this thread http://forum.hibernate.org/viewtopic.php?t=931327.

Quote:
dynamic-update does not affect what happens in the case of update(), unless select-before-update is enabled. Think about it!


Which was not that helpful :)

This is from the docs:

Quote:
select-before-update (optional, defaults to false): Specifies that Hibernate should never perform an SQL UPDATE unless it is certain that an object is actually modified. In certain cases (actually, only when a transient object has been associated with a new session using update()), this means that Hibernate will perform an extra SQL SELECT to determine if an UPDATE is actually required.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 9:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Is it really neccessary to use dynamic-update in your case? There is no performance difference that you can measure and unless you have some weird triggers, I don't see a reason why the whole tuple shouldn't be updated.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 9:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Dynamic-update can only work if the changes occur inside of a session scope. If you reattatch an object using update, Hibernate has no way to determine which properties are dirty, and will update all of them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 9:26 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
Quote:
Dynamic-update can only work if the changes occur inside of a session scope. If you reattatch an object using update, Hibernate has no way to determine which properties are dirty, and will update all of them.


So this means I have to load the object from the database before setting the fields. If I want to have dirty checking.

If I have for example

Code:
class Object

int version
int id
String name
String parent


If I know the primary key then I should be able to do this

Code:
Object object = new Object(434);
object.setName("something")


And this should generate an SQL that only updates the field I set (name).

Or am I missing something?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 9:34 am 
Newbie

Joined: Mon May 31, 2004 8:31 am
Posts: 9
How would hibernate know what has got changed. You implement the setName yourself; after you call all your set and give your object to hibernate to save, there is noway for hibernate to know on what fields you have called the set.

It can only check with the database what the values there for these fields and do a dynamic update based on the database state.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Or you can set select-before-update="true"


Top
 Profile  
 
 Post subject: Sorry for my ignorance
PostPosted: Mon May 31, 2004 12:49 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
sanjibkg wrote:
How would hibernate know what has got changed. You implement the setName yourself; after you call all your set and give your object to hibernate to save, there is noway for hibernate to know on what fields you have called the set.

It can only check with the database what the values there for these fields and do a dynamic update based on the database state.


I thought hibernate did some enhancements to the class so that it can detect changes in all cases. I guess I have to learn CGLIB and ASM. I'm going to read this http://www.open-lab.com/ppolsinelli/hibernateWorks.pdf maybe it will make things clearer.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 12:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hibernate does _not_ do any enhancements to your base class. The only thing it does is - if you use proxys - generate a new subclass of your class in memory to use as a proxy. Just use your debugger and check it, the classes you get back are just normal instances of your class if you don't use proxys.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 1:07 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
michael wrote:
Hibernate does _not_ do any enhancements to your base class. The only thing it does is - if you use proxys - generate a new subclass of your class in memory to use as a proxy. Just use your debugger and check it, the classes you get back are just normal instances of your class if you don't use proxys.


So with a proxy I would get the behaviour I need?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 1:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No. You can not use dynamic-update in combination with detatched objects.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 1:27 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
ok thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 1:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
yes, you can, use select-before-update="true"


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 31, 2004 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Okay, of course :)


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.