2014-10-02 223 views
0

我有文章和作者對象。作者可以有很多文章,而是文章只能有1名作者:映射nhibernate xml多對一關係

public class Author 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 

public class Article 
{ 
    public virtual int Id {get;set;} 
    public virtual string Title {get;set;} 
    public virtual Author Author {get;set;} 
} 

這是我到目前爲止有:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       namespace="Core.Domain.Model" 
       assembly="Core"> 

    <class name="Author" table="Author" dynamic-update="true"> 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native"/> 
    </id> 
    <property name="Name" column="Name" type="string"/> 
    </class> 
</hibernate-mapping> 



<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       namespace="Core.Domain.Model" 
       assembly="Core"> 

    <class name="Article" table="Article" dynamic-update="true"> 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native"/> 
    </id> 
    <property name="Title" column="Title" type="string"/> 
    <one-to-one name="Author" 
     class="Author" 
     constrained="true"/> 
    </class> 
</hibernate-mapping> 

該電流代碼拋出的錯誤是:

Cannot insert the value NULL into column 'AuthorId', table 'NHibernate101.dbo.Article'; column does not allow nulls. INSERT fails. 

如何正確映射它們?

回答

0

上面用例是非常simliar到記錄例如:

最典型的映射(無但任何)我會使用是:

C#

public class Author // new collection mapping 
{ 
    public virtual int Id { get; set; } 
    public virtual IList<Article> Articles { get; set; } 
    ... 
} 

public class Article // as is 
{ 
    public virtual int Id {get;set;} 
    public virtual Author Author {get;set;} 
    ... 
} 

XML

<class name="Author" table="Author" dynamic-update="true" batch-size="25"> 
    ... 
    <bag 
     name="Articles" 
     inverse="true" 
     lazy="true" 
     batch-size="25" 
     cascade="all">    
     <key column="Author_ID"/> 
     <one-to-many class="Article"/>    
    </bag> 

... 
<class name="Article" table="Article" dynamic-update="true" batch-size="25"> 
    ... 
    <many-to-one name="Author" column="Author_ID" not-null="true" /> 

我們有什麼既關係映射結束,每一個新的文章必須有正確設置參考作者(使所有寫操作的工作)。所以這應該是任務

var author = ... 
var article = ... 
author.Articles.Add(article); 
article.Author = author; 

這是必須在創建過程中。後來NHibernate會爲我們加載所有這些設置(如果需要,請延遲)

0

在這種情況下,您需要多對一,而非一對一。您的作者可以有很多文章,但文章只能有作者。見NHibernate Reference Documentation, 5.1.10. many-to-one

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        namespace="Core.Domain.Model" 
        assembly="Core"> 

    <class name="Author" table="Author" dynamic-update="true"> 
     <cache usage="read-write" /> 
     <id name="Id" column="Id" type="int"> 
      <generator class="native" /> 
     </id> 
     <property name="Name" column="Name" type="string" /> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       namespace="Core.Domain.Model" 
       assembly="Core"> 

    <class name="Article" table="Article" dynamic-update="true"> 
     <cache usage="read-write"/> 
     <id name="Id" column="Id" type="int"> 
      <generator class="native"/> 
     </id> 
     <property name="Title" column="Title" type="string"/> 
     <many-to-one name="Author" column="Id" not-null="true" class="Author" /> 
    </class> 
</hibernate-mapping>