Tuesday, 15 February 2011

c# - How do I resolve circular dependencies in mappings? -



c# - How do I resolve circular dependencies in mappings? -

i have 2 tables have circular dependency. title has elements, , in turn each element points next title in path.

however, when phone call buildsessionfactory() on instance of nhibernate.cfg.configuration receive next exception:

an association table element refers unmapped class: elementdao

i believe result of circular dependencies (it can't map title first because refers element, , can't map element without knowing title).

question: how combine 2 mappings in order resolve circular dependencies?

relevant info (and code snippets) follows:

title

id (primary key) name

element

id_title (foreign key) name id_title_child (foreign key)

they represent info construction so:

my code:

titledao:

public class titledao{ public virtual int id { get; set; } public virtual string name { get; set; } public virtual icollection<elementdao> elements { get; set; } }

titledao mapping:

<class name="titledao" table="title" lazy="true"> <id name="id"> <generator class="identity"/> </id> <property name="name"/> <set name="elements" table="element" lazy="true"> <key column="id_title"/> <many-to-many class="elementdao"/> </set> </class>

elementdao:

public class elementdao{ public virtual string name { get; set; } public virtual titledao kid { get; set; } }

elementdao mapping:

<class name="elementdao" table="element" lazy="true"> <property name="name"/> <many-to-one name="child" class="titledao" column="id_title_child"/> </class>

as understand model, have one-to-many relationship title element mapped many-to-many. alter to:

<class name="titledao" table="title" lazy="true"> <id name="id"> <generator class="identity"/> </id> <property name="name"/> <set name="elements" table="element" lazy="true"> <key column="id_title"/> <!-- alter one-to-many --> <one-to-many class="elementdao"/> </set> </class>

c# nhibernate hbm

No comments:

Post a Comment