3

我有以下数据库表,我为此示例进行了简化:

网页ID、URLIdentifier

WebpageMetaData Webpage_id 关键字

webpage_id 是表的主键和返回网页表的外键。

然后我使用 Fluent NHibernate 及其自动映射功能将这些映射到以下 POCO 类:

public class Webpage
{
    public virtual int Id { get; set; }
    public virtual string UrlIdentifier { get; set; }
    public virtual WebpageMetaData WebpageMetaData { get; set; }

}

public class WebpageMetaData
{
    public virtual int Id { get; set; }
    public virtual string Keywords{ get; set; }
    public virtual Webpage Webpage { get; set; }

}

我想将这些实体映射为一对一,因此我覆盖了自动映射,如下所示:

public class WebpageMap : IAutoMappingOverride<Webpage>
{        
    public void Override(AutoMapping<Webpage> mapping)
    {
        mapping.HasOne(w => w.WebpageMetaData)
            .Not.Constrained()
            .Fetch.Join()
            .Cascade.All();
    }
}

public class WebpageMetaDataMap : IAutoMappingOverride<WebpageMetaData>
{
    public void Override(AutoMapping<WebpageMetaData> mapping)
    {
        mapping.Id(w => w.Id, "Webpage_id").GeneratedBy.Foreign("Webpage");

        mapping.HasOne(w => w.Webpage)
            .ForeignKey("Webpage_id")
            .Constrained()
            .Fetch.Join()
            .Cascade.None();
    }
}

我已经测试了这些映射和级联规则,它们适用于选择、插入和更新。但是,当我执行 WebpageMetaData 的删除时,我得到一个异常说

“已删除的对象将被级联重新保存(从关联中删除已删除的对象)[WebpageMetaData#524]”

停止异常的唯一方法是在保存之前执行以下操作:

webpageMetaData.Webpage.WebpageMetaData = null;

所以我必须从我的父母那里为自己设置一个空引用??!这似乎是错误的?我的映射错了吗?

谢谢。

更新 1: 请查看 Fluent 生成的 NH 映射,以防这对核心 NHibernator 有帮助:)

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Webpage`">
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <one-to-one cascade="all" class="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="false" fetch="join" name="WebpageMetaData" />
    <property name="UrlIdentifier" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="UrlIdentifier" />
    </property>    
  </class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="EveryPage.Core.Domain.WebpageMetaData, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`WebpageMetaData`">
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
      <column name="Webpage_id" />
      <generator class="foreign">
        <param name="property">Webpage</param>
      </generator>
    </id>
    <one-to-one cascade="none" class="EveryPage.Core.Domain.Webpage, EveryPage.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" constrained="true" fetch="join" foreign-key="Webpage_id" name="Webpage" />
    <property name="Keywords" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Keywords" />
    </property>
  </class>
</hibernate-mapping>
4

2 回答 2

1

我认为您的映射不太正确。如果您查看NH 文档,他们使用一对一的父子关系示例涉及多对一映射(具有唯一性=true)和一对一映射,而不是一对一的映射。(一对一映射是针对两个表使用共享 PK 关联的情况)

我建议您尝试更改映射以更好地匹配示例,以查看是否修复了级联。

于 2010-01-05T01:01:43.703 回答
0

嗯,Cascade-all-delete-orphan 也许?

于 2010-01-13T17:30:46.817 回答