Hi,
I have a bidirectional many-to-one relationship in a parent/child (email/recipients) table.
In the parent table (email) I have a list of items (recipients). When I remove an item at the end of the list, the row is simply deleted from the recipient table. That is fine.
When I remove a recipient within the list (i.e. not the last one), Hibernate should remove the recipient from the table and it should update the index number of the following items.
What seems to happen is that Hibernate starts by updating the index numbers of the following items and then delete the recipient which I have removed from the list.
This does not work for me because in my Legacy tables there is a constaint on the USERID, NAME, SEQNO (which is my list's index)
So... my question is: Would there be a way for me to tell Hibernate, that it should remove the recipients from the table before updating the seqno of the recipients remaining?
Or is there an other way to do all this?
Thanks you so much for your help!
Jean
Hibernate version: 3.0.4
Mapping documents:
<class name="Email" table="T_EMAIL" >
<composite-id name="ID" class="EmailID" >
<key-property name="userID" column="USERID"/>
<key-property name="name" column="NAME"/>
</composite-id>
<list name="recipients" cascade="all-delete-orphan" inverse="true">
<key>
<column name="USERID"/>
<column name="NAME"/>
</key>
<index column="SEQNO"/>
<one-to-many class="Recipient"/>
</list>
</class>
<class name="Recipient" table="T_RECIPIENT" dynamic-update="true" dynamic-insert="true">
<composite-id name="ID" class="RecipientID">
<key-many-to-one name="alert" class="Alert">
<column name="USERID" length="8"/>
<column name="NAME" length="8"/>
</key-many-to-one>
<key-property name="emailAddress" column="RECIPIENT"/>
</composite-id>
<property name="seqNo" column="SEQNO" not-null="true" type="int" update="true" insert="true"/>
</class>
Code between sessionFactory.openSession() and session.close():
Code:
EmailDAO emailDAO = new emailDAO();
Email email = emailDAO.getByUserIDQNumber("ABCD", "0022");
email.removeRecipient("jean@yyy.com");
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
The generated SQL (show_sql=true):My table TRECIPIENT contains the following fields
Code:
USERID NAME SEQNO RECIPIENT
====== ==== ===== =========
ABCD 0022 1 jean@xxx.com
ABCD 0022 2 jean@yyy.com
ABCD 0022 3 jean@zzz.com
** Unfortunately ** there exist a unique constrain on userid, name. seqno
When I try to remove the second row
The folowing SQL is executed
Code:
update TQRECIPIENT set SEQNO=? where USERID=? and NAME=? and RECIPIENT=?
binding '3' to parameter: 1
binding 'PRDSJT' to parameter: 2
binding '0022' to parameter: 3
binding 'JEAN.TREMBLAY@GG.COM' to parameter: 4
SQL Error: 1, SQLState: 23000
ORA-00001: unique constraint (%s.%s) violated