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
|