Two Laptop Bag*
The moose likes Object Relational Mapping and the fly likes Hibernate + locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate + locking" Watch "Hibernate + locking" New topic
Author

Hibernate + locking

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Can Hibernate ensure certain level of optimistic or other sort of locking on a entity(i.e entry in a table)??

Or is it that you need to resort to your own ways for this locking??


Kishore
SCJP, blog
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
Hibernate has built-in support for optimistic locking.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Loren Rosen:
Hibernate has built-in support for optimistic locking.


Can u elaborate and if the object(or internal data) is being locked does hibernate generate a availability exception(status) or something of that sort???
Loren Rosen
Ranch Hand

Joined: Feb 12, 2003
Posts: 156
The usual way to do this is to have a version property on the object, a <version> tag in the mapping file, and a version column in the table in the database. This is incremented very time the object is updated, and hibernate can check that the version numbers match when you do an update. If they don't it will throw a StaleObjectStateException.

You can also have it do the same thing with a timestamp column.
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Loren Rosen:
The usual way to do this is to have a version property on the object, a <version> tag in the mapping file, and a version column in the table in the database. This is incremented very time the object is updated, and hibernate can check that the version numbers match when you do an update. If they don't it will throw a StaleObjectStateException.

You can also have it do the same thing with a timestamp column.


So, we can define optimistic locking on a class level, during updates??
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Kishore Dandu:
So, we can define optimistic locking on a class level, during updates??
Yes, each class can be set up differently if you like. Optimistic locking is also enforced during delete, and clearly it makes no sense for insert.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate + locking