Consider following entities (getters, setters, equals and hashCode are omitted for brevity):
According to the specification of JPA 2.0 (section 11.1.36), removing a child entity from the parent's children collection should lead to it being removed from the database. However, following code leaves an orphaned child entity stored in the database:
Removing a parent entity however works fine (no orphans remain in the database).
I have attached a JUnit test case illustrating the behavior. I ran the test against another persistence provider, where the orphan was removed as expected.
I have also included the Hibernate-equivalent of the test (using Hibernate's native API instead of JPA). This code also cleans up the orphan as expected. Thus the bug seems to be related to the JPA implementation (EntityManager etc).