-->
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 associations in many-to-many relation
PostPosted: Tue Dec 27, 2011 7:12 am 
Newbie

Joined: Tue Dec 27, 2011 6:46 am
Posts: 3
Hi, I'm new to Hibernate and I'm facing problem I can't find solution for.

I have two tables: price and discount and one association table - price_discount. Price and discount have many-to-many relation. Relation is defined in mapping classes like that:

in Price.java
Code:
  @ManyToMany(
    targetEntity=package.name.Discount.class))
  @JoinTable(
    name="price_discount",
    joinColumns = @JoinColumn(name="priceid"),
    inverseJoinColumns = @JoinColumn(name="discountid"))
  public Set<Discount> getDiscounts() {
    return discounts;
  }


in Discount.java
Code:
  @ManyToMany(mappedBy="discounts")
  public Set<Price> getPrices() {
    return prices;
  }


What I want is to be able to remove price or discount with all records in association table (and only them). When I delete price, everything works fine, but when I try to delete discount, hibernate throws an exception that discount is still referenced in price_discount table. How can I define this relation to enable removing associations from both directions?


Top
 Profile  
 
 Post subject: Re: Deleting associations in many-to-many relation
PostPosted: Thu Dec 29, 2011 5:57 am 
Beginner
Beginner

Joined: Tue Aug 16, 2005 3:44 pm
Posts: 33
Yamk wrote:
What I want is to be able to remove price or discount with all records in association table (and only them). When I delete price, everything works fine, but when I try to delete discount, hibernate throws an exception that discount is still referenced in price_discount table. How can I define this relation to enable removing associations from both directions?

Hi :)

You might try to remove all the prices from the discount before deleting it with something like

Code:
discount.getPrices().clear();


or, I'm not sure if you have to iterate over discount.getPrices() and say

Code:
price.getDiscounts().remove(discount);


-Kaj :)


Top
 Profile  
 
 Post subject: Re: Deleting associations in many-to-many relation
PostPosted: Mon Jan 02, 2012 6:39 am 
Newbie

Joined: Tue Dec 27, 2011 6:46 am
Posts: 3
Thanks for your answer. I was looking for the way to do this automatically and found out its rather impossible.

Price entity is kind of responsible for relationship and thats why associations are removed without problems. When removing discounts, I need to do this manually.


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.