• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Automatic Versioning

 
Wayne Kidd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation seems to me to be quite sparse when talking about automatic versioning of persistent objects. Can anyone describe for me what actually goes on when you choose to use this technique to manage Optimistic locking. How does the application become aware that the row has changed underneath it. Where do the Transaction methods come into play.

Wayne
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Automatic versioning is a simple technique for assuring data integrity.

Consider the following: clients A and B load the same record R. After a time A commits back R with data changed, and after a while B commits too .. changing data of R - but not from the state of R after commit A.

Automatic versioning does the following: a field is used to version the row data. In the above scenario A and B obtain the record R with version t. After the commit A the version is changed, so when B tries to commit the check upon the version fails.

This is somehow similar to a simple one using timestamps - which is not recommended.

./pope

PS: I hope I was able to express my thoughts :-).
 
Wayne Kidd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:
Automatic versioning is a simple technique for assuring data integrity.

Consider the following: clients A and B load the same record R. After a time A commits back R with data changed, and after a while B commits too .. changing data of R - but not from the state of R after commit A.

Automatic versioning does the following: a field is used to version the row data. In the above scenario A and B obtain the record R with version t. After the commit A the version is changed, so when B tries to commit the check upon the version fails.

This is somehow similar to a simple one using timestamps - which is not recommended.

./pope

PS: I hope I was able to express my thoughts :-).


Thank you.
What I am wondering is:
1). How does client B know that the commit is failing?
2). What has the mapping developer done (in the xml file) to make framework cause item 1 to fail the commit and notify client B.

Wayne
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. an exception is thrown upon saveOrUpdate(), update() call: StaleObjectStateException.

2.You have the possibility to describe the column used for versioning. Please check Chapter 10.4 for details on optimistic concurrency control and for details on mapping: Chapter 5.1 (5.1.7 version).

./pope
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) Hibernate issues an update statement like

UPDATE FOOS SET BAR='bar', VERSION=2
WHERE ID=1234 AND VERSION=1

(2) Hibernate checks the JDBC row count, and throws a StaleObjectStateException if no rows were updated
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gavin what about delete? Is the same mechanism used? (I think it would be)

./pope
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gavin King:
Hibernate checks the JDBC row count, and throws a StaleObjectStateException if no rows were updated

Is there a way to force the update by ignoring the version field? I know we can simulate it in the client by rereading the data and reapplying the changes, but it would be easier if the framework supported it.
 
Wayne Kidd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:
1. an exception is thrown upon saveOrUpdate(), update() call: StaleObjectStateException.

2.You have the possibility to describe the column used for versioning. Please check Chapter 10.4 for details on optimistic concurrency control and for details on mapping: Chapter 5.1 (5.1.7 version).

./pope


Thanks.
If someone were to rewrite the 10.4 section, they might want to describe the things that you have told me and that Gavin described in his response.

Wayne
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. If Gavin agrees I could submit a patch for documentation with the discussion reformated. Or we can include it on the wiki part.

Gavin must decide :-).

./pope
 
Ogi K
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The part that's not clear is who increments the version here? Is the application responsible for it before it calls update, or does hibernate do it? It seems to me that the app. cannot do the increment of the version because how would then Hibernate compare this incremented version to what's in the database? Is there any way the app. can do the increment, like for example, using a sequence table?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ogi Kavazovic:
The part that's not clear is who increments the version here?

This is strictly the responsibility of the framework. As well, each object effectively has its own version number sequence. There's no need for version numbers to be unique in a table, though that can be handy for other uses.

Each update increments the version number by one and checks that it hadn't been changed since it was read.
 
Mat Robinson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to this point, is there a way or could there be a way to not update the existing version but to always insert new rows with the new version number. Thus automatically generating an auditable database. Probably in conjunction with a logical delete?

This is vital in a number of applications I work on and up to this point it has always had to be done manually - and is nearly always a pain.
 
Rashmi Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....I am new to hibernate. I want to know what is the difference in between session.connection().commit and transaction.commit.

if I want to use automatic version, then what should I use to commit.

Thanks
 
Zeeshan Samdani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mat Robinson:
Adding to this point, is there a way or could there be a way to not update the existing version but to always insert new rows with the new version number. Thus automatically generating an auditable database.


This is really an application requirement, not a data storage requirement. If you�re doing this commonly, do you use helper methods, a static helper class or factor the data into a couple of classes?

Zeeshan Samdani
www.powermapjdo.com
 
Diana McHenry
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've got what I thought would be a common problem, but after Googling incessantly I've not found any similar posts.

I have two objects joined in a one-to-one relationship (same primary key value). My problem is, when object B only is updated, I want the version of object A to update as well. This is unfortunately an inflexible business requirement.

I've found no way to configure Hibernate to do this, and specifying the versions myself causes far to much trouble to deal with.

Does anyone know a way to trick Hibernate into thinking an object is "dirty" when it isn't? I tried adding an Interceptor to the session but was unable to make it do anything useful here. I also don't know of any way to turn off Hibernate's pre-update inspection process.

Any thoughts or guidance would be very appreciated.

Thanks!

Diana
 
Steven Jiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is

When I do a hql select query joining a few table together. hibernate does update version field???

Anybody know why? I reckon only update a table will trigger an update on the version field, right?

Thanks
Steve
 
Manish fico
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,
does any of your entities in HQL follow inheritance? If thats the case, hibernate will try to sync the versions of the parent and child even in read operation.
Regards,
Manish
 
Steven Jiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish fico wrote:Steve,
does any of your entities in HQL follow inheritance? If thats the case, hibernate will try to sync the versions of the parent and child even in read operation.
Regards,
Manish


Hi Manish.

Thanks for that, we do have parent abstractbusinessobject as abstract , but not mapped in the mapping file. so I dont think it will have influence on the token increase.

If you think about other clue, please let me know.

Steven
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic