Advertisement

07.24.2007 at 01:35PM PDT, ID: 22718250
[x]
Attachment Details

Hibernate - many to many relationship update

Asked by joeyoungkc in Java Programming Language, J2EE

Tags: hibernate, many, delete

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
Start Free Trial
[+][-]07.24.2007 at 10:58PM PDT, ID: 19563018

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, J2EE
Tags: hibernate, many, delete
Sign Up Now!
Solution Provided By: Bart_Cr
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.25.2007 at 10:10PM PDT, ID: 19571947

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32