These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Merge in NHibernate
PostPosted: Wed Mar 10, 2010 6:52 am 
Newbie

Joined: Wed Mar 10, 2010 6:00 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Merge in NHibernate
PostPosted: Wed Mar 10, 2010 8:26 am 
Newbie

Joined: Wed Mar 10, 2010 6:00 am
Posts: 3
BTW, when i use SaveOrUpdateCopy, everything seems to work just fine. only 2 issues:
1. this method is marked as deprecated
2. I have implemented code in a CustomSaveOrUpdateEventListener and when using save or update i don't reach this code.

Is there a solution to the issue that merge and SaveOrUpdateCopydoes not attach the objects to the session?


Top
 Profile  
 
 Post subject: Re: Merge in NHibernate
PostPosted: Wed Mar 10, 2010 11:02 am 
Newbie

Joined: Wed Mar 10, 2010 6:00 am
Posts: 3
Ok, so i found the solution :)
Apparently i can specify more than one value in the cascade property
for example:
Quote:
<many-to-one name="C" column="C_id" class="C" cascade="save-update,merge"/>


Now the only problem is that the save or update event listener on the save operation on the C class doesn't hit unless i save it first.

Please, any ideas ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.