2011-01-13 153 views
1

我有這樣的:一對多在NHibernate的映射問題

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(); 
+0

指數,因爲你正在使用列表映射所需。使用套件或包包映射消除它。 – 2011-01-13 19:12:57

回答

2

NHibernate的有它在Java中,其中一個子類「擴展」一個基類的根,當定義不同hbm文件的hierach時,這有時是一個有用的映射元素。

您看到錯誤是您將用戶電影映射爲「子類」的方式的原因。這是令人困惑的NHib,因爲你沒有擴展任何東西。刪除列表中的「子類」節點,該錯誤將消失。

順便說一句,傑米是正確的,爲什麼列表索引是必需的。列表映射很好,但除非有一個令人信服的理由不這樣做,否則我通常需要爲我的一對多關係設置語義,如下面的示例中的hbm。

HTH,
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> 
+0

在NHibernate工具本身和相關模式之間有一大堆材料。通過應用在其他地方學到的知識並在此處或在[NHib用戶組論壇](http://groups.google.com/group/nhusers?hl=zh-CN)上提問,您的做法是正確的。我也在閱讀這本書 - 這是一本好書! – Berryl 2011-01-14 01:14:31