We are having a probem with modificaction of detail data in a many-to-many master-detail mapping.
We have a class Entity, with a set of contact numbers associated using a mapping table, so we have Entity, TelephoneNumber and EntityTelephoneNumber tables.
When we create an entity and add numbers to the set and call persist all is 100% and we think it is working just nicely.
The problem comes when updating the number. We load the data from the database using load on the session, then we update the number property of the telephone number and call saveorupdate on the entity. (I know we can load the number alone and call saveorupdate on that, yes it will work, but I cannot write code to traverse the object graph and call update on every property, this is supposed to work, not?)
We are expecting Hibernate to generate SQL that will update the number, which is does. Splendid, but right at the end it generates a statement "delete from EntityTelephoneNumber where TelephoneNumberId = 234" or something like that. This just makes no sense at all.
Looking at the entity all seems 100%, it has a ISet<TelephoneNumber> which has the number in there, the number and Entity have correct Id's and all the object data seems 100%. So why would Hibernate decide to delete the link table entry, thus leaving the TelephoneNumber record orphaned?
We are using NHibernate V2.0.1.4000.
The actual scenario has a little more complexity with the specializations of the Entity class and lots of other properties, but to give the idea this is more or less what the mappings look like if reduced to the problem area:
<class table="Entity" abstract="true" name="test.entity" lazy="false" polymorphism="explicit" discriminator-value="0">
<id name="Id" column="EntityId" type="Int32" unsaved-value="0"> <generator class="identity"> </id>
<set name="TelephoneNumbers" table="EntityTelephoneNumbers" cascade="all" lazy="false"> <key> <column name="EntityId" not-null="true"/> </key> </set>
The mapping for Telephone Numbers is basically:
<class name="TelephoneNumbers" table="TelephoneNumbers" lazy="false"> <id name="Id" column="TelephoneNumberId" type="Int32" unsaved-value="0"> <generator class="identity"> </id>
<property name="Number" type="string"> </class>
|