Hi,
I have posted a question on StackOverflow (https://stackoverflow.com/posts/28370940), without success.
Can you please tell me if the folloing is a known bug ?
====
We have an issue that sound like a bug in Hibernate dealing with
orphanRemoval=true for a
oneToMany List (with index).
Here is the simplified mapping :
Code:
public class ParentClass {
[...]
@OneToMany(cascade = ALL, mappedBy = "parent", orphanRemoval = true)
@OnDelete(action = OnDeleteAction.CASCADE)
@Fetch(FetchMode.JOIN)
@OrderColumn(name = "pos", nullable = false)
public List<ChildClass> getChildren() {
return children;
}
}
And the child class :
Code:
public class ChildClass {
[...]
@ManyToOne
@JoinColumn(nullable = false, name = "parent_id")
public ParentClass getParent() {
return parent;
}
}
Given this mapping, the following scenario is failing :
1. Fetch a *Parent* from DB
2. Add a child to it
3. Get something else from DB (not same entity), producing a partial flush
4. Remove the child from the parent
5. Leave the transaction
Here is the code
Code:
@Transactional
public void test() {
// 1)
ParentClass parent = entityManager.find(ParentClass.class, "some-id");
// 2)
ChildClass child = new ChildClass(parent);
parent.getChildren().add(child);
// 3)
entityManager.find(SomethingElse.class, "2");
// 4)
parent.getChildren().remove(child);
}
In that case, the child allocation is inserted into DB and not removed at te end of the transaction.
However, if we don't do step 3), the child allocation is correctly not persisted in DB
Is that a bug ? A wrong mapping ?
Is there a workaround for it ?