-->
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.  [ 2 posts ] 
Author Message
 Post subject: Obsolete Rows in Association / Join Table
PostPosted: Fri Apr 16, 2004 2:52 pm 
Newbie

Joined: Wed Jan 07, 2004 5:40 pm
Posts: 6
Location: United Kingdom
I have two classes which have a many to many bi-directional relationship with each other.




Code:
<class name="Pattern" table="PATTERN">
   
      <id name="id" column="id" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
      
      <property name="name" column="name" type="string" not-null="true" length="100"/>

      <!-- many to many bi-directional with Style -->
      <set name="styles" table="STYLE_PATTERN_JT">
            <key column="pattern_id"/>
            <many-to-many column="style_id" class="Style"/>
        </set>
       
   </class>
   
   
   <class name="Style" table="STYLE">
      
      <id name="id" column="id" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
       
      <property name="name" unique="true" column="name" type="string" not-null="true" length="100"/>
      
      <!-- many to many bi-directional with Pattern -->
      <set name="patterns" table="STYLE_PATTERN_JT" inverse="true">
            <key column="style_id"/>
            <many-to-many column="pattern_id" class="Pattern"/>
        </set>

   </class>



I create two Patterns and two Styles:

    PatternA
    PatternB
    StyleA
    StyleB

I then associate both patterns with both styles, so I have the following associations in the join table:
Code:
   mysql> select pattern_id, style_id from style_pattern_jt;
   +------------+----------+
   | pattern_id | style_id |
   +------------+----------+
   |          1 |        1 |
   |          2 |        1 |
   |          1 |        2 |
   |          2 |        2 |
   +------------+----------+


If I then (using Hibernate Session) delete the pattern with an id of 1, The pattern is deleted from the pattern table and I am left with the following in the join table:

Code:
   mysql> select pattern_id, style_id from style_pattern_jt;
   +------------+----------+
   | pattern_id | style_id |
   +------------+----------+
   |          2 |        1 |
   |          2 |        2 |
   +------------+----------+


Which seems correct to me.

However, if I create the associations as above, but delete a style instead, for example deleting style with id number 1, the style is deleted as expected, but there are still four association rows in the join table:
Code:
   mysql> select pattern_id, style_id from style_pattern_jt;
      +------------+----------+
      | pattern_id | style_id |
      +------------+----------+
      |          1 |        1 |
      |          2 |        1 |
      |          1 |        2 |
      |          2 |        2 |
      +------------+----------+


The first two rows are now 'orphans' as there is no longer a style with id of 1. This does not seem correct to me.

I have tried moving the inverse="true" to the other end of the association in the mapping file and I get the opposite results, in other words, deleting style now works and deleting pattern does not.

I have tried using cascade="delete", but this does not solve the 'orphan' problem

Don't get me wrong, Hibernate still navigates my associations correctly, i.e. it ignores the first two rows in the join table, but I am concerned that it is not good housekeeping to have obsolete rows in my join table


could someone have a look at my mapping files and let me know if I have made a mistake, or if it is expected behaviour,


many thanks

Daniel


ps I am using MySQL 3.23.39 Hibernate 2.1.1


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 11:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is acceptable to have inverse="true" on neither end.


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