Hibernate version: 3.2
mysql Ver 14.7
I have a many-to-many relation from a Plan to an array of Step.
These are my hibernate mappings:
Code:
<hibernate-mapping default-access="field">
<class name="testHibernate.Plan" table="PLANS" >
<id name="id">
<generator class="increment" />
</id>
<list name="stepList" cascade="all" lazy="false">
<key column="plan_id" foreign-key="id" />
<list-index column="list_index"/>
<many-to-many class="testHibernate.Step" column="step_id"/>
</list>
</class>
<class name="testHibernate.Step" table="STEPS" >
<id name="id">
<generator class="increment"/>
</id>
<property name="parameterX" type="int"/>
</class>
</hibernate-mapping>
Now, when I save the same plan (a plan with 2 steps) a 2nd time,
then I get these DB tables:
Code:
mysql> select * from PLANS;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
mysql> select * from STEPS;
+----+------------+
| id | parameterX |
+----+------------+
| 1 | 15 |
| 2 | 43 |
+----+------------+
2 rows in set (0.00 sec)
mysql> select * from stepList;
+---------+---------+------------+
| plan_id | step_id | list_index |
+---------+---------+------------+
| 2 | 2 | 1 |
| 2 | 1 | 0 |
+---------+---------+------------+
2 rows in set (0.00 sec)
The problem is, that the PLANS table still holds 2 plans, whereas
the stepList table deleted the references to the first plan. This
makes the database inconsistent.
I would expect a stepList table like this:
Code:
+---------+---------+------------+
| plan_id | step_id | list_index |
+---------+---------+------------+
| 1 | 2 | 1 |
| 1 | 1 | 0 |
| 2 | 2 | 1 |
| 2 | 1 | 0 |
+---------+---------+------------+ (this is a hand edited table!)
Can I change something in my mappings to get the desired behaviour?
(BTW: Please don't tell me, that I just should not write the same
object a 2nd time, because I am just a persistence provider, and
can not be sure, that the users of it never call it for saving with
an already saved object a 2nd time.)
Maybe I should give the sql log during the 2nd write as well:
Code:
Hibernate: insert into PLANS (id) values (?)
Hibernate: update STEPS set parameterX=? where id=?
Hibernate: update STEPS set parameterX=? where id=?
[b]Hibernate: delete from stepList where plan_id=?[/b]
Hibernate: insert into stepList (plan_id, list_index, step_id) values (?, ?, ?)
Hibernate: insert into stepList (plan_id, list_index, step_id) values (?, ?, ?)
It is this delete statement I don't want to have.
Thanks for your help in advance!
Tom[/b][/code]