Hi,
I am having a lot of difficulty with the merge method of NHibernate in .Net 4.
Let's say i have 4 tables, all with the same fields (ID,Version,Name,Associations) : A,B,C,D
A has a list association of Bs.
B has an association to A and To C.
And C an association to D.
Because i transfer my object between tiers, i use DTO objects between layers.
Here is the mapping of class C:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="Model"
namespace="Model.Entities" >
<class name ="C" table="C">
<id name="Id" type="Int64">
<generator class="native"/>
</id>
<version name="Version"/>
<property name="Name"/>
<many-to-one name="D" column="D_id" class="D" cascade="save-update"/>
</class>
</hibernate-mapping>
I insert to the database a full graph (all objects and their relations), and from now on i work with corresponding DTO objects.
on the dto object i remove the association to B and create a new association with a new B (full graph).
after converting the DTO back to NHibernate classes, and try to merge these changes, i get an exception:
Quote:
object references an unsaved transient instance - save the transient instance before flushing. Type: Model.Entities.C, Entity: Model.Entities.C
and this exception vary according to the cascade value in the mappings:
Code:
cascade="all"
could not delete: [Model.Entities.C#1][SQL: DELETE FROM C WHERE Id = ? AND Version = ?]
Code:
cascade="merge" (exeption on the graph save)
object references an unsaved transient instance - save the transient instance before flushing. Type: Model.Entities.C, Entity: Model.Entities.C
the only way this scenario work as expected (the merge will delete b and will insert the new association with the entire graph) is by marking the cascade option to be s"all-delete-orphan" (but i don't want to delete orphans:))
hope i was clear with my explemation. i will be more than happy to send you further information, this is a main scenarion when working with detached objects and i just cant seem to make this work.
thanks,
Maorino