【问题标题】:one-to-one mapping is not working with 2nd-Level-Cache一对一映射不适用于 2nd-Level-Cache
【发布时间】:2012-08-18 13:51:36
【问题描述】:

我已经用 NHibernate3 声明了休耕映射:

使用 FluentNHibernate

public class ActivityMap : ClassMap<Activity> {
    public ActivityMap() {
        this.Table("Activity");
        this.Cache.ReadWrite();
        this.Version(x => x.ObjectVersion);
        this.Id(x => x.Id).GeneratedBy.Assigned();

        // snipp

        this.HasOne(x => x.AppointmentRecurrence).Cascade.Delete();
    }
}

public class AppointmentRecurrenceMap : ClassMap<AppointmentRecurrence> {
    public AppointmentRecurrenceMap() {
        this.Table("AppointmentRecurrence");
        this.Cache.ReadWrite();     
        this.Version(x => x.ObjectVersion);
        this.Id(x => x.Id).GeneratedBy.Foreign("Activity");

        // snipp

        this.HasOne(x => x.Activity).Constrained();
    }
}

正在生成闲置的 hbm 映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" lazy="false" name="Prayon.Entities.Activity, Prayon.Entities, Version=1.0.0.867, Culture=neutral, PublicKeyToken=null" table="Activity">
    <cache usage="read-write" />
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="assigned" />
    </id>
    <!-- snipp -->
    <one-to-one cascade="delete" class="Prayon.Entities.AppointmentRecurrence, Prayon.Entities, Version=1.0.0.867, Culture=neutral, PublicKeyToken=null" foreign-key="FK_Activity_AppointmentRecurrence" name="AppointmentRecurrence" />
  </class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" lazy="false" name="Prayon.Entities.AppointmentRecurrence, Prayon.Entities, Version=1.0.0.867, Culture=neutral, PublicKeyToken=null" table="AppointmentRecurrence">
    <cache usage="read-write" />
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="foreign">
        <param name="property">Activity</param>
      </generator>
    </id>
    <!-- snipp -->
   <one-to-one class="Prayon.Entities.Activity, Prayon.Entities, Version=1.0.0.867, Culture=neutral, PublicKeyToken=null" constrained="true" foreign-key="FK_AppointmentRecurrence_Activity" name="Activity" />
  </class>
</hibernate-mapping>

现在,当我选择活动(带有可缓存选择)时,我在 NHibernate-Profiler 中看到,NHibernate 正在从二级缓存中正确获取每个活动,但它会为每个活动做一个选择到约会复发。我该怎么办,它也会从缓存中获取 AppointmentRecurrence ? 我尝试在一对一关系上设置缓存属性,但似乎不受支持。

【问题讨论】:

  • 你确定解决方案了吗?

标签: nhibernate caching fluent-nhibernate hibernate-mapping one-to-one


【解决方案1】:

您的映射的Constrained 似乎确实存在问题。根据 Ayende 的one-to-one 指南:

还有一点需要注意的是,我们必须用 foreign-key=”none” 来指定它,否则 NHibernate 的 Schema Export 功能会为我们创建两个外键,这会创建一个不允许我们插入的循环引用数据库中的任何内容。

显然,FK 实际上不存在于您的数据库中,但错误配置可能会导致缓存出现问题:

Fluent中指定foreign-key="none"的方式:

this.HasOne(x => x.Activity).Constrained().ForeignKey();

【讨论】:

  • mmhh - 这不起作用。 ForeignKey() 正在数据库上生成一个 Key。 ForeignKey("none") 不会 - 但实体尚未从 chache 加载:-(
  • 您是否尝试在关系上明确设置.Fetch.Select()? (来自here 的随机想法)(老实说,我通常只是声明一对一的关系,因为我发现一对一有太多的怪癖。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2022-01-22
  • 2013-10-12
  • 2019-12-01
  • 1970-01-01
  • 2014-11-13
  • 2012-05-25
相关资源
最近更新 更多