Using part of a composite primary key in a composite foreign key in NHibernate -
we have big db (~200 tables) entirely uses composite primary keys , composite foreign keys, using single "base table" every other table inherits part of primary key:
- parent has single column primary key parentid
- child has composite primary key (parentid, childid) , foreign key parentid
- nephew has composite primary key (parentid, nephewid), foreign key parentid , foreign key (parentid, childid)
and on. until managed whole shebang orm framework of our own, we're considering using nhibernate, i've been assigned learn (i've downloaded v2.1.2).
the mappings: child
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="assembly" namespace="namespace"> <class name="child" table="child"> <composite-id name="idchild" class="childid"> <key-many-to-one name="parent" column="parentid" class="parentid"></key-many-to-one> <key-property name="id" column="childid" type="int32"></key-property> </composite-id> <!--simple properties--> <set name="nephews" table="nephew"> <key> <column name="parentid"></column> <column name="childid"></column> </key> <one-to-many class="nephew"/> </set> </class> </hibernate-mapping>
nephew
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="assembly" namespace="namespace"> <class name="nephew" table="nephew"> <composite-id name="idnephew" class="nephewid"> <key-many-to-one name="parent" column="parentid" class="parent"></key-many-to-one> <key-property name="id" column="nephewid" type="int32"></key-property> </composite-id> <many-to-one name="child" class="child"> <column name="parentid"></column> <column name="childid"></column> </many-to-one> <!--simple properties--> </class>
i can post classes if want, i'll omit them brevity (as i'll omit mapping parent, since doesn't have problems). every property virtual, every mapping file embedded resource, every composite id has own class overrides equals , gethashcode.
the problem can't save instance of nephew, initialized through simple new nephew()
, passed on _session.save()
, because system.indexoutofrangeexception: invalid index n sqlparametercollection count=n.
.
the column duplicated in mapping parentid
. removing many-to-one
mapping in nephew
, set
mapping in child
, related properties works fine.
i found several posts reporting exception, , appropriate in case seems this one, gives me gut feeling current schema infeasible nhibernate. please tell me i'm wrong :-)
notes:
- i'm not using fluent right now, though may option, preferred learning basics first;
- yes, realize composite primary keys big pain in ass. db has passed through several hands through years, not skilled ones, before refactoring count 10 000
unfortunately, won't able map relationship in current form, have inferred.
however, there's workaround. instead of mapping nephew.child
many-to-one, map childid
regular property, , use query when need retrieve child.
there's 1 more chance hack. if , if nephew.child not null , not mutable, map key referencing child instead of parent:
<class name="nephew" table="nephew"> <composite-id name="idnephew" class="nephewid"> <key-many-to-one name="child"> <column="parentid"> <column="childid"> </key-many-to-one> <key-property name="id" column="nephewid"/> </composite-id> </class>
Comments
Post a Comment