-->
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.  [ 15 posts ] 
Author Message
 Post subject: Optimistic locking, timestamp, saveOrUpdate
PostPosted: Tue Jan 27, 2004 8:41 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

I have a problem with optimistic locking using timestamp. I am using MySQL.

My hbm file looks like this:

<hibernate-mapping>

<class name="package.Clazz"
table="THE_TABLEr">

<!--id name="id"
type="int"
column="ID">
<generator class="identity"/>
</id-->

<id name="id"
type="int"
column="ID"
unsaved-value="null">
<generator class="identity"/>
</id>

<version name="timeOfLastUpdate"
column="TIME_OF_LAST_UPDATE"
type="timestamp"/>

<property name="name"
type="java.lang.String"
column="name"
not-null="true"
length="50"/>

<property name="homePage"
type="java.lang.String"
column="homePage"
length="100"/>

</class>

</hibernate-mapping>


---------------------------------------------------------------------------------
The Java code:
ses.saveOrUpdate( obj );
ses.flush();
ses.connection().commit();


---------------------------------------------------------------------------------

I query a record in my JSP page, change the name and then push my Save button. I then get the error:

13:16:02,850 INFO [STDOUT] Hibernate: insert into MY_TABLE (TIME_OF_LAST_UPDATE, name, homePage) values (?, ?, ?)
13:16:02,870 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: S1009
13:16:02,870 ERROR [JDBCExceptionReporter] Invalid argument value, message from server: "Duplicate entry 'a' for key 2"
13:16:02,880 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: S1009
13:16:02,880 ERROR [JDBCExceptionReporter] Invalid argument value, message from server: "Duplicate entry 'a' for key 2"
13:16:02,880 ERROR [JDBCExceptionReporter] could not insert: [com.betservant.model.Bookmaker]
java.sql.SQLException: Invalid argument value, message from server: "Duplicate entry 'a' for key 2"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1651)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:889)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:956)


----------------------------------------------------------------------------------
The database column TIME_OF_LAST_UPDATE is of type datetime.

The thing is that I don't want it to do an insert since it is a change of an existing database record. If i change the version tag like this:

<version name="updateVersion"
column="updateVersion"
type="long"/>


then it works. The default value in the database for updateVersion is 0 and it gets accumulated nicely each update. Is it something I missed with dealing with timestamps? Or have I misunderstood something with saveOrUpdate? Do I have to manually set the timestamp on my object before passing it to saveOrUpdate?


Best regards
Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use <timestamp> instead of <version> for timestamp versioning.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 8:47 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Him


Thanks. Why is that? Something wrong with version?`

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 5:38 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
What I am thinking is what it says in Chapter 5.1.8 of the reference:

Quote:
Note that <timestamp> is equivalent to <version type="timestamp">.


Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 5:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Version is for version (version + 1 when updated)
timestamp is for... timestamp

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 6:26 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Ok, so what is <version type="timestamp"> used for?

Best regards
Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 6:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
For nothing. It is nonesense. IMHO Hibernate should complain about this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 6:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, the reason is that any Type can actually be used as a version type, as long as the Type also implements VersionType. It is all poymorphic internally.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 7:03 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
But could this really be the reason why Hibernate didn't recognize my record as persistent and therefore tried to do an insert? I mean having <version type="timestamp"....

Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Probably rather a problem with your unsaved-value mapping. But timestamp for a version property is wrong too.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 2:06 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi gloeglm,

1st thing
When I used this

<version name="updateVersion"
column="updateVersion"
type="long"/>


it worked. In both cases I use the unsaved-value="null". But, what does that have to do with the version column? Hmm, feel puzzled here.

=================================

2nd thing
I then look into the adminapp-0.9.2 example which is downloadable on hibernate.org. If you look into src/java/org/hibernate/admin/model/User.hbm.xml you'll find the following:

<id name="id" column="USER_ID"
unsaved-value="null">
<!-- Wrapper Long, so use
unsaved-value="null" instead of "0". -->
<generator class="increment"/>
</id>

and

<!-- Use a timestamp for optimistic
locking -->
<version name="timeOfLastUpdate"
column="TIME_OF_LAST_UPDATE"
type="calendar"/>



This must be working musn't it?
I haven't tested the adminapp application but I think I will today.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 7:12 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
What I also wonder if I choose to use <timestamp> for optimistic locking, do I have to set the value of the database column myself?

When I used the column of type LONG it had an initial value of 0, zero, in the database.

Best regards


Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 10:00 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Solved....

I just wanted to say that I solved it. The solution really indicates that my question really really was posted in the correct group, i.e. Beginners.

The thing I missed to do was to carry the value of the timestamp-version field all the way up to the web-clien and then down to the server when the user made a submit in the web form.

In my case I use Struts, so the thing I added was to carry the timeOfLastUpdate value in my ActionForm object ... and then assigned the timeOfLastUpdate to the value object, i.e. the object which is coupled to the hbm.xml file.

I got it to work with the <timestamp>-tag. I did not test it with the <version>-tag and type=timestamp. But that should also work.

Best regards and Thanx a lot for all your help. You are great people!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 10:05 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
As a general rule, do you think is correct to put such important datas on the presentation tier ?
I usually put there only the identifier of my objects, to be sure no one can change on the client so important data.
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 10:44 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

Well, first my intention of this thread was optimistic locking. What I then wonder is what field you would use to accomplish optimistic locking? By definition, it has to be a field that can change. I suppose that your id's never change.

Second, the fields that I used in this example are not exposed in the web browser, they only travel along with the ActionForm object. How do you mean that the fields can be changed?

Best regards

Lasse


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