-->
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.  [ 3 posts ] 
Author Message
 Post subject: Deleting an entity with a @ManyToMany relationship
PostPosted: Tue Mar 18, 2008 3:41 pm 
Newbie

Joined: Tue Mar 18, 2008 3:32 pm
Posts: 4
I have two classes that are related as follows:



Code:
class Cart {
   
    private Long id;
    private Set<Item> items;

    @ManyToMany(targetEntity = Cart.class, fetch = FetchType.EAGER)
    @JoinTable(
        name="cart_item",
        joinColumns = @JoinColumn( name="id"),
        inverseJoinColumns = @JoinColumn( name="id")
    )
    public Set<Item> getItems() {
        return items;
    }

}

class Item {
   private Long id;
}


If I delete an Item from the cart or delete the Cart entirely, both the Cart entity and the association in "cart_item" is removed.

However, if I delete attempt to delete an "Item" instance, I get a failed constraint exception because Hibernate is not deleting any associated entries in the "cart_item" table.

If I delete an "Item", I would expect it to remove items from the "cart_item" table. How can I make this happen?

I've tried adding an association on Item like the following:

Code:
class Item {
    private Long id;
    private Set<Cart> carts;

    @ManyToMany(mappedBy="items")
    public Set<Cart)> getCarts() {
        return carts;
    }
}


but that gave me the same error.

I tried adding CascadeType.REMOVE to either side of the association but it ended up removing the Cart, "cart_item" and Item records which is not what I want.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 9:53 am 
Newbie

Joined: Tue Mar 18, 2008 3:32 pm
Posts: 4
Even if I simplify the code further with:

Code:
class Cart {
    private Set<Item> items;

    @ManyToMany(fetch=FetchType.EAGER)
    public Set<Item> getItems() {
        return items;
    }
}

class Item {
   
}


Hibernate will automatically create a cart_item join table for me.

If I delete a Cart instance, the associated cart_item row goes away

If I delete an Item instance, I get the following

Code:
Hibernate flushing: Could not execute JDBC batch update; uncategorized SQLException for SQL [delete from item where id=?]; SQL state [null]; error code [0]; failed batch; nested exception is java.sql.BatchUpdateException: failed batch


As you can see, when I delete an Item, it is not removing the associated rows in the link table cart_item before it attempts to delete the Item record


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 11:22 am 
Newbie

Joined: Thu Feb 21, 2008 8:39 am
Posts: 15
take a look at my post, it might help.

http://forum.hibernate.org/viewtopic.ph ... 57&start=0


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