映射nhibernate xml多对一关系

时间:2014-10-02 19:49:19

标签: asp.net nhibernate nhibernate-mapping

我有文章和作者对象。作者可以有很多文章,但文章只能有1个作者:

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.

如何正确映射这些?

2 个答案:

答案 0 :(得分:1)

在这种情况下你需要多对一,而不是一对一。您的作者可以拥有多个文章,但文章只能包含一个作者。见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>

答案 1 :(得分:0)

上述用例非常类似于记录的示例:

我将使用的最典型的映射(不包括任何其他)

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" />

我们所拥有的是两个关系端的映射,其中每个新文章必须正确设置对Author的引用(以使所有WRITE操作正常工作)。所以这应该是作业

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

在创建过程中必须这样做。之后NHibernate将为我们加载所有这些设置(如果需要,可以延迟)