简体   繁体   English

映射nhibernate xml多对一关系

[英]mapping nhibernate xml many-to-one relationship

I have the Article and Author objects. 我有文章和作者对象。 An Author can have many articles, but an Article can only have 1 Author: 一个作者可以有很多文章,但是一个文章只能有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;}
}

Here is what I have so far: 这是我到目前为止的内容:

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

The error that this current code is throwing is: 当前代码抛出的错误是:

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

How to I map these correctly? 如何正确映射这些?

You need many-to-one in this case, not one-to-one. 在这种情况下,您需要多对一,而不是一对一。 Your Author can have many Articles, but an Article can have only one Author. 您的作者可以有很多文章,但是一篇文章只能有一个作者。 See NHibernate Reference Documentation, 5.1.10. 参见NHibernate参考文档,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>

The above use-case is very simliar to documented example: 上面的用例与已记录的示例非常相似:

The most typical mapping (without any but) I would use is: 我将使用的最典型的映射(没有任何映射是:

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

What we have is mapping of both relation ends, where every new Article must have properly set reference to Author (to make all WRITE operations working). 我们所拥有的是两个关系端的映射,其中每个新Article必须具有对Author的正确设置的引用(以使所有WRITE操作起作用)。 So this should be the assignment 所以这应该是分配

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

This is must during creation. 在创建期间必须这样做。 Later NHibernate will load all these settings for us (lazily if needed) 稍后,NHibernate将为我们加载所有这些设置(如果需要,可以延迟加载)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM