?
Solved

Hibernate - many to many relationship update

Posted on 2007-07-24
2
Medium Priority
?
5,495 Views
Last Modified: 2008-01-09
Hi,

We are using spring and hibernate with mysql, we have 3 tables:
---------------------------------------------------
CREATE TABLE PRODUCT_LINE (
  product_line_id INTEGER UNSIGNED NOT NULL,
  product_line_name VARCHAR(64) NULL,
  PRIMARY KEY(product_line_id)
)
TYPE=InnoDB;

CREATE TABLE PRODUCT_FAMILY (
  product_family_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  product_family_name VARCHAR(64) NULL,
  PRIMARY KEY(product_family_id)
)
TYPE=InnoDB;

CREATE TABLE PRODUCTFAMILY_PRODUCTLINE_LINK (
  product_family_id INTEGER UNSIGNED NOT NULL,
  product_line_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY(product_line_id)
    REFERENCES PRODUCT_LINE(product_line_id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
  FOREIGN KEY(product_family_id)
    REFERENCES PRODUCT_FAMILY(product_family_id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION
)
TYPE=InnoDB;
---------------------------------------------------
In my .hbm files, we have:

    <class name="com.pcn.persistence.domain.ProductFamily" table="PRODUCT_FAMILY">
      ...          
        <set name="productLines" inverse="true" table="PRODUCTFAMILY_PRODUCTLINE_LINK">      
            <key>
                <column name="product_family_id" not-null="true" />
            </key>
            <many-to-many entity-name="com.pcn.persistence.domain.ProductLine">
                <column name="product_line_id" not-null="true" />
            </many-to-many>
        </set>
    </class>
-----------------
    <class name="com.pcn.persistence.domain.ProductLine" table="PRODUCT_LINE">

        <set name="productFamilies" inverse="true" table="PRODUCTFAMILY_PRODUCTLINE_LINK" cascade="merge,save-update,delete" lazy="true">
            <key>
                <column name="product_line_id" not-null="true" />
            </key>
            <many-to-many entity-name="com.sun.pcn.persistence.domain.ProductFamily" >
                <column name="product_family_id" not-null="true" />
            </many-to-many>
        </set>
</class>
---------------------------------------------------
so when I try to delete the link between and product_family and product_line, i do this:

ProductLine pl = session1.get(ProductLine .class, ProductLineId);
ProductFamily pf = getFromSomeWhere();
if (pl.getProductFamilies().contains(pf))
   pl.ProductFamilies.remove(pf);

session1.merge(pl);
-----------------------------------------------------

However, the link won't remove, the row in table "PRODUCTFAMILY_PRODUCTLINE_LINK" still exist.
I want to keep the product_family and product_line and just remove the link, how can i do it??

Please help.
Joe
0
Comment
Question by:joeyoungkc
2 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 200 total points
ID: 19563018
You should not use inverse="true" on both ends of the relationship.

0
 
LVL 10

Expert Comment

by:kupra1
ID: 19571947
You need to break the link from the other side too.
i.e. something of this sort:
pf.ProductLines.remove(pl);
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question