-->
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.  [ 13 posts ] 
Author Message
 Post subject: dynamic-update
PostPosted: Sun May 30, 2004 11:13 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
Hibernate 2.1.2
Spring 1.0.1
OsCache
Mysql 4.0

I have a class called Node. It has dynamic-update enabled.

<class
name="project.Node"
table="node"
dynamic-update="true"
dynamic-insert="true"
>

In my unit test I do like this:

Node node = new Node(new Integer(94540));
node.setName("test");
session.saveOrUpdate(node);

I'm expecting the following SQL to be executed:
update nodes set name = test where node_id=94540

But instead this sql is executed:
insert into nodes (name, version, text, etc, etc) values....

What is wrong?


Here's the debug info:
Code:
DEBUG  - version unsaved-value strategy NULL
DEBUG  - saveOrUpdate() unsaved instance
DEBUG  - saving [project.Node#<null>]
DEBUG  - executing insertions
DEBUG  - Seeding: 0
DEBUG  - Inserting entity: project.Node (native id)


Top
 Profile  
 
 Post subject: Found one problem
PostPosted: Sun May 30, 2004 11:22 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
I'm currently looking into optimistic locking and forgot to set the version number.

So if I do like this an SQL update is executed.
node.setVersion(new Integer(16));
node.setName("test");

But I still have the problem with dynamic-update.

Hibernate executes this:
update nodes set name=?, version=?, text=?, etc, etc

But I'm expecting it to update just the two columns I updated.
update nodes set name=?, version=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 11:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you do not have to set the version number.

looks like you have some broken get/set pairs.


Top
 Profile  
 
 Post subject: Version number
PostPosted: Sun May 30, 2004 11:40 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
I'm building a webapplication so I have a version field on the form that is used to edit the node object.

Code:
<input type="hidden" name="version" value="$!node.version"/>


If I don't set the version how would Hibernate know the version of the edited object? Or are you not allowed to call setVersion ever?

Should I do manual version checking instead?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 11:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you map a property with the <version> tag, Hibernate manages the version number.


Top
 Profile  
 
 Post subject: version tag
PostPosted: Sun May 30, 2004 11:50 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
I'm using the version number tag.

Quote:
<version
column="version"
name="version"
type="integer"
unsaved-value="null"
/>


Could you explain if my approach to versioning is correct in a webapplication.
Or even better how you would do it.


Top
 Profile  
 
 Post subject: NPE
PostPosted: Sun May 30, 2004 12:10 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
And when not setting the version number I get a NPE.

Code:

java.lang.NullPointerException
   at net.sf.hibernate.type.IntegerType.next(IntegerType.java:49)
   at net.sf.hibernate.engine.Versioning.increment(Versioning.java:25)
   at net.sf.hibernate.impl.SessionImpl.getNextVersion(SessionImpl.java:2589)
   at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2523)
   at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422)
   at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
your unsaved-value mapping is incorrect, or you have a broken get/set pair


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 1:14 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
gavin wrote:
your unsaved-value mapping is incorrect, or you have a broken get/set pair


I fixed the mapping by specifying java.lang.Integer as type for the version.

But I still have the problem with dynamic-update. I just set the value of one column but still it tries to update or insert the values of all columns.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
dynamic-update does not affect what happens in the case of update(), unless select-before-update is enabled. Think about it!


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 1:34 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
gavin wrote:
dynamic-update does not affect what happens in the case of update(), unless select-before-update is enabled. Think about it!


I will do the version checks manually like in option three in the documentation:
http://www.hibernate.org/hib_docs/refer ... optimistic

I thought Hibernate would do it automatically but I guess not. At least with my configuration.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 1:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
huh??

of course hibernate does it automatically


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 30, 2004 2:31 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
Could you please explain how? If you're developing a webapplication how would you do it.

emmanuel described a solution here http://forum.hibernate.org/viewtopic.php?t=928177 and I'm trying to get it to work. Without any luck.


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