c# - Fluent NHibernate HasManyToMany with a discriminator column specifying relationship type -
i'm using fluent nhibernate , i'm trying map many-to-many property bypasses join table. tricky thing is, join table has column determines type of relationship is.
for purpose of question, let's have person table , relation table.
persontable (personid, name, etc) relationtable (relationtype, personida, personidb)
i want introduce collection property in person class each type of relationship e.g. sons, daughters, etc.
hasmanytomany<person>(x => x.sons) .table("relationtable") .parentkeycolumn("personida") .childkeycolumn("personidb") .where("relationtype='a_is_father_of_b_boy'"); hasmanytomany<person>(x => x.daughters) .table("relationtable") .parentkeycolumn("personida") .childkeycolumn("personidb") .where("relationtype='a_is_father_of_b_girl'");
the above mappings working reading database not inserting, example:
person john = persondao.getbyname("john"); // sons , daughters loaded fine based on mappings above john.sons.add(new person("jack")); // let's add new son persondao.save(john); // fails because relationtype null
basically when saving jack john's new son in relationtable need have relationtype populated "a_is_father_of_b_boy", not happening. directive .where("relationtype='a_is_father_of_b_boy'") effective loading not saving.
any ideas? think similar discriminator attribute subclasses.
any appreciated. thanks.
i say, pointed out in comment:
i have relationtable mapped 2
many-to-one
references person (as persona , personb). usingone-to-many
(hasmany), how suggest map sons , daughters (bothlist<person>
) in person class taking account discriminator values above
so pairing object in eyes
public class personrelation { // pairing table must contain key column, surrogated id public virtual int id { get; protected set; } // must. public virtual person parent { get; set; } public virtual person child { get; set; } public virtual string relationtype { get; set; } }
there must key table. inject identity column sql server.. have surrogated (business domain independent) key.
here our person entity
public class person { ilist<personrelation> _sons; ilist<personrelation> _daughters; .., public virtual ilist<personrelation> sons { { return _sons ?? (_sons = new list<personrelation>()); } set { _sons = value; } } public virtual ilist<personrelation> daughters { { return _daughters?? (_daughters= new list<personrelation>()); } set { _daughters= value; } } }
the mapping sons (daughters same):
hasmany<personrelation>(x => x.sons) // .table("relationtable") // not needed - part of personrleation mapping .keycolumn("personida") .where("relationtype='a_is_father_of_b_boy'") .inverse() .cascade.alldeleteorphan() ;
that work, if assure, when adding son, set relationtype
var parent = ...; // new, loaded var child = ...; // new, loaded var relation = new personrelation { parent = parent; child = child; relationtype = "a_is_father_of_b_boy"; }; parnet.sons.add(relation);
this must either part of businese layer addson() or poco public method...
note: can map reverse end of relation ... without relationtype filtering:
public class person { ... above public virtual ilist<personrelation> parents { get; set; } hasmany<personrelation>(x => x.parents) // instead of // .keycolumn("personida") // need column .keycolumn("personidb") ;
Comments
Post a Comment