c# - Setting nhibernate property-ref on one-to-many to property other than primary key fails -
i receiving exception when set property-ref in xml file.
initializing[domain.entities.r#12345]-failed lazily initialize collection of role: domain.entities.r.lp, no session or session closed
lp.hbm.xml ---------- <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="domain.entities" assembly="domain"> <class name="lp" table="lp"> <id name="id"> <column name="id" sql-type="int" not-null="true"/> </id> <property name="anotherfield"/> <property name="paymentdate"/> <property name="paymentamount"/> </class> </hibernate-mapping> r.hbm.xml --------- <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="domain.entities" assembly="domain"> <class name="r" table="r"> <id name="id"> <column name="id" not-null="true"/> </id> <property name="anotherfield"/> <set name="lp"> <key column="anotherfield" property-ref="anotherfield" /> <one-to-many class="domain.entities.lp" not-found="ignore" /> </set> </class> </hibernate-mapping> iqueryable<entities.r> query = _db.query<entities.r>(); var query2 = _db.query<entities.lp>().tolist(); var queryresults = query.tolist(); iesi.collections.generic.iset<entities.lp> lp; try { lp = queryresults.first().lp; <--- fails exception } catch (exception ex) { console.writeline(ex.message); } { var lp2 = _db.query<entities.lp>().take(100); <-- works fine }
what don't understand why lp2 set fine, lp fails? know data model isn't ideal, it's have work now. if remove property-ref xml file in nhprof see make calls sql table (with wrong value no data back), doesn't fail. occurs when have property-ref set.
any appreciated. first run nh.
the error message giving answer. set of lp objects in r class lazy loaded default. means need access lp collection within session.
typically, getting isessionfactory , calling opensession in using block. access collection in using block , should fine.
Comments
Post a Comment