-->
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.  [ 1 post ] 
Author Message
 Post subject: many-to-many:hibernate deletes too much
PostPosted: Sun Aug 09, 2009 2:52 pm 
Newbie

Joined: Fri Jul 10, 2009 1:19 pm
Posts: 2
So here's the deal,

I have a 3 tables, test, test2, and test_test2_join (the association table between test and test2)
hibernate is doing its thing and when I have objects from test and test2 that are related, it automatically puts a row in the association table. Its great. The problem is when I delete a row.

Here's a junit test that illustrates the problem:
Code:

   public void testOneSide()
   {
      try
      {
         Test aTest = new Test();
         Test2 aTest2=this.myTest2Home.findById(1);//existing row in Test2
         
         aTest.setTestId(5);
         aTest.setDescription("test 5");
         
         aTest.getTest2s().add(aTest2);
         aTest2.getTests().add(aTest);
         this.myTestHome.attachDirty(aTest);/*<------this causes a row to be inserted into Test, and test_test2_join
                                                                                        which is exactly what I want*/
         Test aTest_Next = this.myTestHome.findById(5);
         System.out.println(aTest.getDescription());
         for(Iterator aIter=aTest.getTest2s().iterator();aIter.hasNext();)
         {//yes yes printing during a junit test is bad form. 
            Test2 aTest2_next=(Test2)aIter.next();
            System.out.println(aTest2_next.getTest2Id()+"|"+aTest2_next.getTestDesc()+"|");
         }
         this.myTestHome.delete(aTest_Next);/*<---------------This causes the following to be deleted:
                                                                                                       the row just inserted into Test (good)
                                                                                                       the row just inserted into test_test2_join (good)
                                                                                                       the row that already existed in Test2 (bad)
                                                                                                       All rows in Test that were related to the above Test2 row (bad)
                                                                                                       all rows in test_test2_join that described these relationships(bad)*/
      } catch (RuntimeException re)
      {
         re.printStackTrace();
         fail("testSessionMgt:" + re.getLocalizedMessage() + ":::" + re.getMessage()
               + ":::" + re.getStackTrace());
      }
   }


So if I have the following rows in Test
Code:
test_id   description
1            sdads
**2            ffff
3            aaa


The following rows in Test2
Code:
test2_id   test desc
**1             dadf
2             fwad


Code:
And the following rows in test_test2_join
test_id   test2_id
**2               1
1               2
3               2


the above test will end up deleting the rows I have marked with asterisks. This is bad. Don't want this. I only want the rows inserted in the junit test to be deleted. What is the very stupid mistake I am making? Any help is appreciated.


Also here is the mapping file, if that is helpful

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 16, 2009 11:10:18 PM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="dbtables.Test" table="test" catalog="class">
        <id name="testId" type="java.lang.Integer">
            <column name="test_id" />
            <generator class="assigned" />
        </id>
        <property name="description" type="string">
            <column name="description" length="10" not-null="true" />
        </property>
        <set name="test2s" inverse="true" lazy="true" table="test_test2_join" cascade="all" >
            <key>
                <column name="test_id" />
            </key>
            <many-to-many entity-name="dbtables.Test2">
                <column name="test2_id" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


thanks in advance

trippy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.