i'm trying add new cross-reference tables relate accounts packages, , coming across strange problem when try save account.
this cross-reference table has attributes of own, that's why used collection of them instead of packages.
what happens when call saveorupdate on account, nhibernate apparently decides try fill in other account properties (like product) null objects. i've verified commenting out new mapping no longer causes product setter hit. (it hits other many-to-one mapped objects, it's nothing special product, that's 1 example).
it's important note happens on insert only. on both update , insert, we'll set productfk form input. on update account.product set retrieving account database. on insert, new account created productfk set not product. suspect it's product being null causes nhibernate try fill in before insert, don't understand why adding new mapping triggers that.
here's account mapping file:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="kpa.mko.dal" namespace="kpa.mko.dal.entities.accounts"> <class name="account" table="accounts"> <id name="accountid" type="int32" unsaved-value="0"> <generator class="identity" /> </id> <property name="parentaccountfk" /> <many-to-one name="parentaccount" column="parentaccountfk" insert="false" update="false" /> <property name="productfk" not-null="true" /> <many-to-one name="product" column="productfk" insert="false" update="false" /> <property name="districtfk" not-null="true" /> <many-to-one name="district" column="districtfk" insert="false" update="false" /> <property name="address1" /> <snipped out other plain properties address1 /> <set name="accountpackagexrefs" table="account_package_xref" inverse="true"> <key column="accountfk"/> <one-to-many class="account_package_xref" /> </set> <sql-insert> exec up_gen_account_insert @parentaccountfk = ?, @externalaccountid = ?, @clientaccountnumber = ?, @accountname = ? ,@logofk = ?, @accounttypefk = ?, @accountstatusfk = ?, @isbillableaccount = ?, @productfk = ?, @hotlinkaccount = ? ,@formgroupfk = ?, @districtfk = ?, @primaryengineerfk = ?, @secondaryengineerfk = ? ,@address1 = ?,@address2 = ?,@city = ?,@statefk = ?,@zip = ?,@country = ?,@phone = ?,@fax = ?,@tollfreenumber = ? ,@billingaddress1 = ?,@billingaddress2 = ?,@billingcity = ?,@billingstatefk = ?,@billingzip = ?,@billingcountry = ? ,@billingcontactname = ?,@billingcontactphone = ?,@billingcontactemail = ? ,@termdate = ?,@visitvalue = ?, @accountindustrytypefk = ?, @createdbyfk = ?,@createddate = ?,@modifiedbyfk = ?,@modifieddate = ? </sql-insert> </class>
and account_package_xref mapping:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="kpa.mko.dal" namespace="kpa.mko.dal.entities.accounts"> <class name="account_package_xref" table="account_package_xref"> <id name="accountpackageid" type="int32" unsaved-value="0"> <generator class="identity" /> </id> <property name="accountfk"/> <property name="packagefk"/> <property name="primaryconsultantfk"/> <property name="secondaryconsultantfk" /> <property name="createdbyid" column="createdbyfk" not-null="true" update="false" /> <property name="createddate" not-null="true" update="false" /> <property name="modifiedbyid" column="modifiedbyfk" not-null="true" /> <property name="modifieddate" not-null="true" /> <many-to-one name="account" column="accountfk" not-null="true" insert="false" update="false"/> <many-to-one name="package" column="packagefk" not-null="true" insert="false" update="false"/> </class> <sql-query name="account_pacakge_xref_getforaccountandpackage"> <return class="account_package_xref"/> exec account_package_xref_getforaccountandpackage @accountid = :accountid, @packageid = :packageid </sql-query> so when execute on insert
nhibernatesession.saveorupdate(entity); i see breakpoints hit in setter here in account, null value (which, of course, fails.)
public virtual servicesproduct product { { return _product; } set { _product = value; productfk = value.productid; } } but when have new mapping included. so, it's trying add new collection don't understand.
i add setting product in addition productfk when construct new account, that's workaround , doesn't explain why adding new relationship causes system act differently respect product (and properties) of account.
the point in orm world (ok, maybe nhibernate)
- assignment of
valuetype(string including)treated is. not later re-checked (re-evaluated, re-assigned) again, when original source of value has changed - assigning of
referencetypedifferent. reference (product) assigned property represents c# reference. @ moment product_id not resolved , not assigned. so, nhibernate waitisession.flush(), order insert statements - required.- firstly product inserted...
- its
product_idretrieved... - and used insert of root entity -
product_idvalue
to make working have change mapping
// not right way orm <property name="productfk" not-null="true" /> <many-to-one name="product" column="productfk" insert="false" update="false" /> <property name="districtfk" not-null="true" /> <many-to-one name="district" column="districtfk" insert="false" update="false" /> which valuetype being managed nhiberante, reference management:
// object relations mapped. orm <property name="productfk" not-null="true" insert="false" update="false" /> <many-to-one name="product" column="productfk" /> <property name="districtfk" not-null="true" insert="false" update="false" /> <many-to-one name="district" column="districtfk" /> also, because there magic sql-insert store procedures (are still forced db guy that?, no way how avoid it?), suggest adjust entity mapping this
// fields product _product; // properties public virtual int productfk { get; set; } public virtual product product { { return _product; } set { _product = value; productfk = _product != null ? _product.productid : 0; } }
Comments
Post a Comment