one-to-one mapping is not working with 2nd-Level-C

2019-04-13 18:21发布

I have declared the fallowing mapping with NHibernate3:

with 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();
    }
}

which is generating the fallowing hbm-mapping:

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

Now, when i select Activities (with a cachable-select), I see in the NHibernate-Profiler, that NHibernate is getting each Activity correct out of the 2nd-Level-Cache, but it will do for each Activity a select to AppointmentReccurrence. What can I do, that it will take the AppointmentReccurrence also from the cache? I have tried to set a cache-attribute on the one-to-one relationsship, but it seems not be supported.

1条回答
做自己的国王
2楼-- · 2019-04-13 18:34

You do seem to have a problem with the Constrained side of your mapping. According to Ayende's one-to-one guide:

Something else to note is that we must specify this with foreign-key=”none”, because otherwise NHibernate’s Schema Export feature would create two foreign keys for us, which would create a circular reference that wouldn’t allow us to insert anything into the database.

Clearly the FK doesn't actually exist in your database, but the misconfiguration could conceivably cause issues with the cache:

The way to specify foreign-key="none" in Fluent:

this.HasOne(x => x.Activity).Constrained().ForeignKey();
查看更多
登录 后发表回答