Hi, I'm having trouble stopping updates to a collection affecting the version of the parent entity, where I would expect @OptimisticLock(excluded = true) to do the trick. I'm using JPA, Hibernate EntityManager 3.4.0.GA.
The model features
- Unidirectional many-to-one child-parent relationship.
- The child class has a reference to its parent.
- The parent class does not have a collection of children.
The pseudo-code for the add-child operation is:
begin transaction;
find Parent;
create Child;
child.setParent(parent);
persist(child);
commit transaction;
When I execute this concurrently I get a StaleObjectStateException, as each operation tries to lock the parent. I tried exclusion from optimistic lock in the child's annotation:
Code:
@ManyToOne(targetEntity = Parent.class)
@OptimisticLock(excluded=true)
@JoinColumn(name = "parentId")
@ForeignKey(name = "fk_child_parent")
public Parent getParent() {
return parent;
}
without success. I then added a collection of children to the parent, with exclusion specified there too:
Code:
@OneToMany
@OptimisticLock(excluded = true)
Set<Child> getChildren() {
return children;
}
also without success.
Can anyone tell me what's wrong here?
Thanks,
Richard