Dear all,
I have two tables. Table A and Table B has a one-to-many relationship. Each table A record can have several records in B, and table B itself has no primary key.
Therefore i only defined mapping file for Table A and inside it there is a collection attribute like this:
<set name="bSet" table="TABLEB" lazy="false" fetch="join" cascade="save-update, delete, all-delete-orphan">
<key>
<column name="colA"/> <!-- an attribute in table A -->
</key>
<composite-element class="tableB">
<property name="colB" type="date" column="COLB" />
<!-- some other table B columns -->
</composite-element>
</set>
which is a set of TableB objects.
Whenever i do an update of the TableA object, the update will be propagated to this collection and update statements on Table B will be generated even if i didn't modify anything in the collection.
For example, there is one tableB object in the collection without any modification and i update the tableA object. After the update statement on Table A, there will be first a delete statement on Table B, followed by insert statement on Table B. The delete statement is to delete the one tableB object and then the record is re-insert into Table B. It seems the result will be the same. However, the delete statement fails to delete the record when any of the fields in the composite-element contains null. The generated delete statement is like this:
delete from TABLEB where colA=? and colB=?
and a null is bind to the colB, which makes it something like
delete from TABLEB where colA=X and colB=null
which fails to delete the target record.
Since the delete fails to delete anything, and the insert proceeds, then there contains duplicate records.
I think one cause of this might because table B doesn't contain a primary key, therefore all columns attributes become part of the where clause and affect it when it is null.
Does anyone encountered that before? Is it really the correct behavior of hibernate? Is there any solution?
Thank you!
|