Is there a way to ensure that a DomainObject graph i.e. a Parent with a collection of children is versioned as a single unit.
Hibernate seems to increment the parent version when a child is added/removed from the parent collection. However it doesn't seem to do so if a child is updated.
Was there a reason this was not done? Because its possible to break Parent-level invariants when concurrently updating a child. e.g. consider the foll object graph:
---------------
Order (invariant: sum of prices of all items should be less than $1000)
|--- OrderItem A - $350, Quantity 1
|--- OrderItem B - $200, Quantity 1
(Total: $550)
- Now 2 users load this Order object at the same time.
- The first user updates the quantity of 'OrderItem A' to 2 and saves the Order
- Simultaneously, the second user updates the quantity of 'OrderItem B' to 2 and saves the Order
Both these updates would go through fine, since different records/objects were updated; however the database is now in an inconsistent state because the invariant at the Order level is broken (the order total is $1100).
---------------
Has anyone come across such a situation? Or is it rare enough that it's ok to ignore?
Is there a way to handle such a situation in Hibernate. We are thinking of incrementing the Parent object's version in an Interceptor whenever a child is updated. Would this cause any other problems? Is there another way?
Thanks,
-Yogi
|