The Ebean throws OptimisticLockException when I recover my bean from DB, add in it a new private owned child, remove this new child, and then save the parent object.
The beans and mappings are:
public class Parent{
@Version
private Integer version;
@PrivateOwned
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List children;
...
}
public class Child{
@Version
private Integer version;
@ManyToOne
private Parent parent;
...
}
The example code could be:
public void test(){
Parent parent = Ebean.find(Parent.class, 1);
Child child = new Child();
parent.getChildren.add(child);
... for some reason, child gets unnecessary ...
parent.getChildren.remove(child);
Ebean.save(parent); // this throws OptimisticLockException
}
Ebean tries to delete the new child from DB but gets nothing since the object has null id and version. Thus the OptimisticLockException is thrown.
I googled a lot these days for some workaround to this issue. My implementation for avoiding the exception and keeping the deal with concurrency is very expensive.
It would be great to have Ebean keeping the orphan removal of PrivateOwned annotation along with some mechanism that avoids trying to delete non persistent children.
My suggestions is that Ebean ignore entities with null version field while removing orphans. Alternatively, the orphanRemoval could do the same.
Best Regards.
Icaro Muniz