我有文章和作者對象。作者可以有很多文章,而是文章只能有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.
如何正確映射它們?