Один ко многим в проблеме отображения nhibernate - PullRequest
1 голос
/ 13 января 2011

У меня есть это:

namespace Demo.Framework.Domain
{
    public class UserEntity
    {
        public virtual Guid UserId { get; protected set; }
    }
}

namespace TDemo.Framework.Domain
{
    public class Users : UserEntity
    {
        public virtual string OpenIdIdentifier { get; set; }
        public virtual string Email { get; set; }
        public virtual IList<Movie> Movies { get; set; }
    }
}

namespace Demo.Framework.Domain
{
    public class Movie
    {
        public virtual int MovieId { get; set; }
        public virtual Guid UserId { get; set; } // not sure if I should inherit UserEntity
        public virtual string Title { get; set; }
        public virtual DateTime ReleaseDate { get; set; } // in my ms sql 2008 database I want this to be just a Date type. Not sure how to do that.
        public virtual int Upc { get; set; }

    }
}

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Demo.Framework"
    namespace="Demo.Framework.Domain">
    <class name="Users">
        <id name="UserId">
            <generator class="guid.comb" />
        </id>
        <property name="OpenIdIdentifier" not-null="true"  />
        <property name="Email" not-null="true" />      
    </class>
    <subclass name="Movie">
        <list name="Movies" cascade="all-delete-orphan">
            <key column="MovieId" />
            <index column="MovieIndex" /> // not sure what index column is really.
            <one-to-many class="Movie"/>
        </list>
    </subclass>     
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Demo.Framework"
    namespace="Demo.Framework.Domain">
    <class name="Movie">      
        <id name="MovieId">
            <generator class="native" />
        </id>
        <property name="Title" not-null="true" />
        <property name="ReleaseDate" not-null="true" type="Date" />
        <property name="Upc" not-null="true" />
        <property name="UserId" not-null="true" type="Guid"/>
    </class>
</hibernate-mapping>

Я получаю эту ошибку:

'extends' attribute is not found or is empty.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: NHibernate.MappingException: 'extends' attribute is not found or is empty.

Source Error:

Line 19:             var nhConfig = new Configuration().Configure();
Line 20:             var sessionFactory = nhConfig.BuildSessionFactory();

1 Ответ

2 голосов
/ 13 января 2011

NHibernate имеет свои корни в java, где подкласс «расширяет» базовый класс, и это иногда является полезным элементом отображения при определении иерархий в разных файлах hbm.

Причина, по которой вы видите эту ошибку, заключается в том, что вы отображаете фильмы пользователя как «подкласс». Это сбивает с толку NHib, так как вы ничего не расширяете. Удалите узел «подкласс», окружающий ваш список, и эта ошибка исчезнет.

Кстати, Джейми прав, почему требуется индекс списка. С отображением списка все в порядке, но если нет веских причин не делать этого, я обычно хочу установить семантику для моих отношений один ко многим, что выглядит как пример ниже в hbm.

НТН,
Berryl

<set access="field.camelcase-underscore" cascade="none" inverse="true" name="Employees">
  <key foreign-key="Employee_Department_FK">
    <column name="DepartmentId" />
  </key>
  <one-to-many class="Employee" />
</set>
...