These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA 2.0 orphanRemoval issue
PostPosted: Tue Oct 04, 2011 4:06 am 
Newbie

Joined: Tue Oct 04, 2011 3:10 am
Posts: 2
Hi,

I have a doubt whether JPA's orphanRemoval attribute works correctly in Hibernate (version 3.6.7) or not.

I have two simple entities "Parent" and "Child" (getters, setters, equals and hashCode are omitted for brevity):

Code:
@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;
}


Here is my test code:

Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPersistenceUnit");
EntityManager em = emf.createEntityManager();

// relate and persist one parent and one child entity

em.getTransaction().begin();
Parent parent = new Parent();
Child child = new Child();
child.setParent(parent);
parent.getChildren().add(child);
em.persist(parent);
em.persist(child);
em.getTransaction().commit();

// disconnect their relationship

em = emf.createEntityManager();
em.getTransaction().begin();
parent = em.find(Parent.class, 1);
parent.getChildren().get(0).setParent(null);
parent.getChildren().clear();
em.getTransaction().commit();


According to the JPA 2.0 specification, the orphaned "Child" entity should be removed from the database. However, this is not the case and it remains in the database with its foreign key set to NULL.

Is this a bug in Hibernate or am I missing something?

Thanks!


Last edited by hibernait on Wed Oct 12, 2011 4:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: JPA 2.0 orphanRemoval issue
PostPosted: Wed Oct 12, 2011 4:30 am 
Newbie

Joined: Tue Oct 04, 2011 3:10 am
Posts: 2
Does anyone have any thoughts on this issue or a working example of orphan removal in Hibernate 3.6.7 using JPA?

I created a JIRA issue (HHH-6709) since to me this appears to be a bug.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.