-->
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.  [ 7 posts ] 
Author Message
 Post subject: merge not updating the join table on a one to many
PostPosted: Mon Jan 15, 2007 4:23 pm 
Newbie

Joined: Fri Oct 06, 2006 10:52 am
Posts: 8
I have a case where doing a merge on an entity with a one to many relationship does not properly update the join table. I currently think this is a major bug in Hibernate.

I have the following entities:

Code:
@Entity
public class Foo
{
  ...
  @OneToMany(cascade = CascadeType.ALL)
  private Set<Bar> bars;
}

@Entity
public class Bar
{
  ...
}


The following works:

Code:
Foo foo = new Foo();
Bar bar = new Bar();
foo.getBars().add(bar);
em.getTransaction().begin();
em.persist(foo); // <<----------------- correctly updates the join table
em.getTransaction().commit();


When this code executes the new Foo and Bar are persisted to the database, an entry is made into the join table, and all is well.

The following fails:

Code:
Foo foo = em.find(Foo.class, id);
Bar bar = new Bar();
foo.getBars().add(bar);
em.getTransaction().begin();
em.merge(foo); // <<----------------- does not update the join table
em.getTransaction().commit();


When this code executes, the new Bar is persisted to the database, but the join table is not updated.

Any comments before I open a bug report?

Bryan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 4:37 pm 
Newbie

Joined: Fri Oct 06, 2006 10:52 am
Posts: 8
I just tried an experiment, and the following works:

Code:
Foo foo = em.find(Foo.class, id);
Bar bar = new Bar();
foo.getBars().add(bar);
em.getTransaction().begin();
em.persist(bar);
em.merge(foo); // <<----------------- correctly updates the join table
em.getTransaction().commit();


Top
 Profile  
 
 Post subject: merge not updating the join table on a one to many
PostPosted: Tue Jan 16, 2007 4:44 pm 
Newbie

Joined: Tue Jan 16, 2007 3:44 pm
Posts: 10
I have the problem too. I use unidirectional one-to-many associations with join tables:
Code:
@Entity
@Name("order")
public class Order implements Serializable {

   private Set<OrderItem> orderItems;

   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(
           name = "ORDER_ITEM",
           joinColumns = @JoinColumn(name = "ORDER_ID"),
           inverseJoinColumns = @JoinColumn(name = "ITEM_ID")
    )
    public Set<OrderItem> getOrderItems() {
      return orderItems;
   }

}

@Entity
@Name("orderItem")
public class OrderItem implements Serializable {


}

1). When I created two new item1 and item2, and save them to new order and run em.persist(order), the order1 and two items were saved to join table (order_item), item entity table, order entity table. (correct).

2). When I tried to delete the order1 and run em.remove(order), the order1, item1 and item2 were deleted from joint table (order_item) and order,item entity tables (correct).

3). I repeated step 1 to create order1, item1 and item2. Then I tried to remove item2 from order1, and add a new item3 to order1, then I run em.merge(order1), the result was that item1 still exists in item table and order_item table (correct), new item3 were added to item table and order_item join table (correct), item2 were deleted from order_order joit table, but item2 still existed in item entity table. (incorrect). It should be deleted.

I don't know whether it is bug or not. Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 5:19 pm 
Newbie

Joined: Fri Oct 06, 2006 10:52 am
Posts: 8
Quote:
3). I repeated step 1 to create order1, item1 and item2. Then I tried to remove item2 from order1, and add a new item3 to order1, then I run em.merge(order1), the result was that item1 still exists in item table and order_item table (correct), new item3 were added to item table and order_item join table (correct), item2 were deleted from order_order joit table, but item2 still existed in item entity table. (incorrect). It should be deleted.


I believe this behavior to be correct. When you remove an item from a collection and merge, the item becomes orphaned. You must explicitly delete the item.

Bryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 6:47 pm 
Newbie

Joined: Tue Jan 16, 2007 3:44 pm
Posts: 10
Hi Bryan,

Yes, you are right. I found that hibernate has cascade setting <cascade="delete-orphan">, in which case the "orphaned" child is deleted, but I coundn't find it in the javax.persistence.CascadeType. However, the org.hibernate.annotations.CascadeType has CascadeType.DELETE_ORPHAN, but it seems it cannot be used in EJB3. Anybody has other ideas, besides that Bryan mentioned "explicitly delete the item"? Thank you, Bryan.

Gus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 6:58 pm 
Newbie

Joined: Tue Jan 16, 2007 3:44 pm
Posts: 10
It seems I found the answer from JBoss EJB3 forum. The solution is:
Code:
@OneToMany(cascade=CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

where @Cascade os a hibernate Cascade object.
http://www.jboss.com/index.html?module=bb&op=viewtopic&t=88062


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 6:11 pm 
Newbie

Joined: Fri Oct 06, 2006 10:52 am
Posts: 8
If anyone is interested, here is a link to the bug I've opened.

http://opensource.atlassian.com/project ... se/EJB-261

I managed to create a trivial testcase to reproduce this bug, and it's attached to the defect.

Bryan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.