-->
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.  [ 1 post ] 
Author Message
 Post subject: a concrete optimistic locking example
PostPosted: Thu Apr 14, 2005 5:02 am 
Newbie

Joined: Wed Apr 13, 2005 8:20 am
Posts: 2
Hi,

Suppose:
class Person {
Long id;
Long version;
String name;
Set<Category> categories;
}

class Category {
Long id;
Long version;
String name;
}

Person has one-to-many association with Category, and both classes are specified to hibernate with versioning for optimistic locking.

Regarding to the CRUD operations;
Create and Read operations are not confusing, since "create" merely instantiate a new object and save it via hibernate and "read" merely loads the instance from DB via hibernate. No version checking etc..

Suppose this is a simple application working with query strings.

Update:
updatePerson?id=1&version=3&name=max&categories=1,4,5

updatePerson command:

Person person = new Person()
person.setId(1)
person.setVersion(3)
person.setName(max)

synchronize person.categories with the categories having Ids 1,4,5

Hibernate.saveOrUpdate(person)

It simply works. Since I havent hold the versions of the categories, I should omit version check on association (optimistic-locking attribute = false ?).

Is the code above a correct way to implement optimistic locking while updating ?

Delete:
deletePerson?id=3&version=5

deletePerson command:

If I have a one-to-many association

Person person = new Person()
person.setId(3)
person.setVersion(5)

Hibernate.delete(person)

It should work. Is above code a correct way to implement optimistic locking while deleting ?

What about many-to-many associations ? How to implement update and remove with them ?

Thanks in advance.

MB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.