nhibernate inheritance mapping issue -
i changed relationship between party & partyname uni-directional, , tests pass now.
but have question final test output:
when test saves student, inserts party, inserts cascading relationship partyname. great.
at end when fetches student, final select.
but right before final select , right after initial insert partyname update:
nhibernate: update partynames set therequiredname = @p0, everythingelse = @p1, contextused = @p2, salutation = @p3, effectivestart = @p4, effectiveend = @p5 partynameid = @p6;@p0 = 'hesh' [type: string (0)], @p1 = 'berryl;;;' [type: string (0)], @p2 = 'student' [type: string (0)], @p3 = null [type: string (0)], @p4 = null [type: datetime (0)], @p5 = null [type: datetime (0)], @p6 = 65536 [type: int32 (0)]
i don't follow why doing that. triggering update?
cheers,
berryl
mapping
<class name="party" table="parties"> <id name="id"> <column name="partyid" /> <generator class="hilo" /> </id> <discriminator column="type" not-null="true" type="string" /> <set access="field.camelcase-underscore" cascade="all" inverse="true" name="names"> <key foreign-key="party_partyname_fk"> <column name="partynameid" /> </key> <one-to-many class="parties.domain.names.partyname, parties.domain" /> </set> <subclass name="student, core.testingsupport" discriminator-value="student" > <property name="number" /> <many-to-one class="course, core.testingsupport" foreign-key="course_fk" name="course"> <column name="somedopeyclassid" index="courseindex" /> </many-to-one> </subclass>
<property name="therequiredname" not-null="true" length="50"/> <property name="everythingelse" /> <property name="contextused" length="50"/> <property name="salutation" length="20"/> <property name="effectiveperiod" type="core.data.nhibernate.usertypes.daterangeusertype, core.data"> <column name="effectivestart"/> <column name="effectiveend"/> </property>
object model code
public class party : entity { ... /// <summary>one or more optional names.</summary> public virtual ienumerable<partyname> names { { return _names; } } private readonly icollection<partyname> _names = new hashset<partyname>(); public virtual void addpartyname(partyname partyname) { if (partyname == null) throw new argumentnullexception("partyname"); _names.add(partyname); } public virtual bool removepartyname(partyname partyname) { if (partyname == null) throw new argumentnullexception("partyname"); return _names.remove(partyname); } } public class student : party { private const string _context = "student"; public virtual personname studentname { { return (personname)this.getnameforcontextof(_context); } set { this.setpartyname(value, _context); } } ... } public static class partynameextensions { public static void setpartyname(this party party, personname personname, string contextused) { if (party == null) throw new argumentnullexception("party"); if (string.isnullorwhitespace(contextused)) throw new argumentnullexception("contextused"); var found = party.getnameforcontextof(contextused); if (personname == null) { if (found != null) party.removepartyname(found); } else { if (found == null) { var partyname = (partyname)personname; partyname.contextused = contextused; party.addpartyname(partyname); } } } public static partyname getnameforcontextof(this party party, string contextused) { if (party == null) throw new argumentnullexception("party"); if (string.isnullorempty(contextused)) throw new argumentnullexception("contextused"); return party.names.singleordefault(x => x.contextused == contextused); } }
passing test (and output)
[test] public void cansaveandload_names() { _student = new student { studentname = nameseeds.devpersonname, }; using (var tx = _session.begintransaction()) { _session.save(_student); tx.commit(); } _session.clear(); student foundstudent; using (var tx = _session.begintransaction()) { foundstudent = _session.get<student>(_student.id); tx.commit(); } assert.that(_student.studentname, is.equalto(nameseeds.devpersonname)); }
output
nhibernate: select next_hi hibernate_unique_key nhibernate: update hibernate_unique_key set next_hi = @p0 next_hi = @p1;@p0 = 2 [type: int32 (0)], @p1 = 1 [type: int32 (0)] nhibernate: select next_hi hibernate_unique_key nhibernate: update hibernate_unique_key set next_hi = @p0 next_hi = @p1;@p0 = 3 [type: int32 (0)], @p1 = 2 [type: int32 (0)] nhibernate: insert parties (number, courseid, type, partyid) values (@p0, @p1, 'student', @p2);@p0 = 0 [type: int32 (0)], @p1 = null [type: int32 (0)], @p2 = 32768 [type: int32 (0)] nhibernate: insert partynames (therequiredname, everythingelse, contextused, salutation, effectivestart, effectiveend, partynameid) values (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 'hesh' [type: string (0)], @p1 = 'berryl;;;' [type: string (0)], @p2 = 'student' [type: string (0)], @p3 = null [type: string (0)], @p4 = null [type: datetime (0)], @p5 = null [type: datetime (0)], @p6 = 65536 [type: int32 (0)] nhibernate: update partynames set therequiredname = @p0, everythingelse = @p1, contextused = @p2, salutation = @p3, effectivestart = @p4, effectiveend = @p5 partynameid = @p6;@p0 = 'hesh' [type: string (0)], @p1 = 'berryl;;;' [type: string (0)], @p2 = 'student' [type: string (0)], @p3 = null [type: string (0)], @p4 = null [type: datetime (0)], @p5 = null [type: datetime (0)], @p6 = 65536 [type: int32 (0)] nhibernate: select student0_.partyid partyid2_0_, student0_.number number2_0_, student0_.courseid somedope4_2_0_ parties student0_ student0_.partyid=@p0 , student0_.type='student';@p0 = 32768 [type: int32 (0)]
Comments
Post a Comment