[hibernate-issues] [Hibernate-JIRA] Created: (HHH-6709) Orphan removal in @OneToMany does not work properly (JPA 2.0)

Damien (JIRA) noreply at atlassian.com
Wed Oct 5 05:09:23 EDT 2011


Orphan removal in @OneToMany does not work properly (JPA 2.0)
-------------------------------------------------------------

                 Key: HHH-6709
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6709
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 3.6.7
         Environment: MySQL 5.1.54
            Reporter: Damien
         Attachments: Tests.zip

Consider following entities (getters, setters, equals and hashCode are omitted for brevity):

{code:borderStyle=solid}
@Entity
public class Parent
{
	@Id
	@GeneratedValue
	private int id;

	@OneToMany(mappedBy="parent", orphanRemoval=true)
	private List<Child> children = new LinkedList<Child>();
}

@Entity
public class Child
{
	@Id
	@GeneratedValue
	private int id;
	
	@ManyToOne
	private Parent parent;
}
{code}

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:

{code:borderStyle=solid}
// assuming there is one parent entity in the db
// containing one child entity in its collection

entityManager.getTransaction().begin();
Parent parent = entityManager.find(Parent.class, parentId);
parent.getChildren().get(0).setParent(null);
parent.getChildren().clear();
entityManager.getTransaction().commit();
{code}

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).

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list