Tuesday, 15 March 2011

nhibernate - Why does cascade SaveUpdate triggers a Delete statement? -



nhibernate - Why does cascade SaveUpdate triggers a Delete statement? -

this related question asked previously.

in request mapping have set saveupdate due , in discount mapping have cascade set none.

there 2 scenarios:

the first new request , new discount. created both added discount request , saved request; works expected.

next scenario new request , existing discount. not working right , i'm unsure why. below sql statements ran in test (omit values):

nhibernate: insert requests nhibernate: select @@identity nhibernate: insert discountrequests (discountid, requestid) values (3, 5); nhibernate: update discounts nhibernate: delete discountrequests discountid = 3;

the final line issue: going , deleting insert line 3. command applying request object session.save(request);, nil else.

would there reason why phone call delete made?

edit:

update code

public void add(request request) { using (isession session = nhibernatehelper.opensession()) { using (itransaction transaction = session.begintransaction()) { session.save(request); transaction.commit(); } } }

discount mapping

<join table="discountrequests" optional="true"> <key column="discountid" /> <many-to-one name="request" column="requestid" cascade="none" /> </join>

request mapping

<join table="discountrequests" optional="true"> <key column="requestid" /> <many-to-one name="discount" column="discountid" cascade="save-update" /> </join>

ttp://stackoverflow.com/questions/14837373/zero-to-one-relationship-nhibernate-mapping

i disagree accepted reply earlier question; see this article explanation of bring together mapping.

i model simple many-to-many relationship (or 2 one-to-manys if discountrequest has additional data). can map collections fields , enforce rule discount has 0 or 1 request (and vice versa) through public property. if it's many-to-many cascade setting should none.

for example:

public class request { private icollection<discount> _discounts; public request() { _discounts = new hashset<discount>(); } public discount { { homecoming _discounts.singleordefault(); } set { _discounts.clear(); if (value != null) { _discounts.add(value); } } }

nhibernate nhibernate-mapping nhibernate-cascade

No comments:

Post a Comment