Question

I have 2 tables that have a circular dependency. A Title has Elements, and in turn each Element points to the next Title in the path.

However, when I call BuildSessionFactory() on my instance of NHibernate.Cfg.Configuration I receive the following exception:

An association from the table Element refers to an unmapped class: ElementDAO

I believe this is a result of circular dependencies (it can't map Title first because it refers to Element, and it can't map Element without knowing Title).

Question: How do I combine two mappings in order to resolve circular dependencies?

Relevant Data (and code snippets) follows:

Title

  • Id (Primary Key)
  • Name

Element

  • id_Title (Foreign Key)
  • Name
  • id_Title_Child (Foreign Key)

They represent a data structure like so: Graph showing relationship of tables

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 Child { 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>
Était-ce utile?

La solution

As I understand your model, you have a one-to-many relationship from Title to Element but it is mapped as many-to-many. Change it 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"/>
      <!-- change this to one-to-many -->
      <one-to-many class="ElementDAO"/>
    </set>
</class>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top