c# - SaveOrUpdate in NHibernate : id is 0 for relater entities -
c# - SaveOrUpdate in NHibernate : id is 0 for relater entities -
i have entity listing
, listingforlease
(short version)
public class listing: entity{//some properties} public class listingforlease: listing { public listingforlease() { listingspaces = new hashedset<listingspace>(); } public virtual iset<listingspace> listingspaces { get; set; } }
and entity listingspace :
public class listingspace : entity { public listingspace(){ } public virtual listingforlease listingforlease{ get; set; } public virtual string name { get; set; } public virtual double size { get; set; } }
entity
sharparch architectural foundation, gives own entities field id
.
for mapping database used nhibernate. listing mapping :
<class name="listing" table="bt_listing" > <id name="id" column="id"> <generator class="native"/> </id> <!-- joined subclasses --> <!-- listing lease --> <joined-subclass name="listingforlease" table="bt_listing_for_lease" proxy="listingforlease" lazy="true"> <key column="id" /> <property name="propertyname" column="property_name" /> .... <set name="listingspaces" table="bt_listing_space" inverse="true" lazy="true" cascade="all-delete-orphan"> <key column="listing_for_lease_id" /> <one-to-many class="listingspace" /> </set> </joined-subclass> </class>
listing space mapping :
<class name="listingspace" table="bt_listing_space" lazy="true"> <id name="id" column="listing_space_id"> <generator class="native" /> </id> <many-to-one name="listingforlease" class="listingforlease" column="listing_for_lease_id" cascade="save-update" /> <property name="name" column="[name]" /> <property name="size" column = "size" /> </class>
problem : when create new listing
, create _listingrepository.saveorupdate((listing)listing);
can new listing id
's of new spaces.
but when update listing (add new spaces) can't id
's of added spaces (id
equal 0). added spaces saved after commiting transaction _listingrepository.dbcontext.committransaction();
this problem in mappings or usual behaviour of nhibernate?
update : adding new space (from viewmodel)
listing = entitymaphelper //map info listingforlease entity .maplistingforleasetoentity((listingforleaseviewmodel)model,(listingforlease)listing, locationdictionary, brokerlist); var listingforlease = (listingforleaseviewmodel)model; var listingspacestoremove = new list<listingforleasespace>(); //edit existing spaces ((listingforlease)listing).listingspaces .where(s => !s.isdeleted).tolist() .foreach(s => { var editedspace = listingforlease.spaces.firstordefault(es => es.spaceid == s.id); if (editedspace != null) { s.name = editedspace.name; s.size = editedspace.size.getvalueordefault(0); // here must go other mappings fields } else { if (s.hasrelatedrecs) { s.isdeleted = true; } else { listingspacestoremove.add(s); } } }); //remove deleted spaces ((listingforlease)listing).listingspaces.removeall(listingspacestoremove); //add new spaces ((listingforlease) listing) .listingspaces .addall(listingforlease.spaces .where(s => s.spaceid == 0).tolist() .select(s => { var retentity = new listingspace { listingforlease = ((listingforlease) listing), name = s.name, //other fields }; homecoming retentity; }).tolist());
i say, essential part here:
<class name="listing" ... > ... <set name="listingspaces" table="bt_listing_space" lazy="true" inverse="true" // here cascade="all-delete-orphan"> <key column="listing_for_lease_id" /> <one-to-many class="listingspace" /> </set>
the collection mapped inverse. means, if experience:
... when update listing (add new spaces) can't id's of added spaces (id equal 0)...
the suspected is, item added collection, not know parent. must proper code turn a profit inverse="true"
:
// listing.... existing 1 var existinglisting = ... // getexistingsomehow(); // new space var space = new listingspace (); // must have assigned parent. must space.listing = existinglisting; // parnet colleciton extended existinglisting.listingspaces.add(space); // save parent, cascade rest session.saveorupdate(existinglisting);
so, if inverse in play... must not forget assign both ends. both must allow nhibernate behave properly...
extend:
in case issue is: when updating parent listing
, newly added items listingspace
have own id equal 0 - until commit called: absolutely ok.
nhibernate work abstraction called isession. not persisted underlying db. check 9.6. flush. if want id's, generated db engine (generator="native") assigned, can anytime phone call explicit
session.flush();
which write changes made in sesion in right order db.
c# nhibernate transactions entity uniqueidentifier
Comments
Post a Comment