一对一映射不适用于二级缓存 [英] one-to-one mapping is not working with 2nd-Level-Cache

查看:119
本文介绍了一对一映射不适用于二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c 

使用NHibernate3宣布下游映射:

$ c> 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-mapping: p>

 < hibernate-mapping xmlns =urn:nhibernate-mapping-2.2> 
class xmlns =urn:nhibernate-mapping-2.2lazy =falsename =Prayon.Entities.Activity,Prayon.Entities,Version = 1.0.0.867,Culture = neutral,PublicKeyToken = null表= 活动 >
< cache usage =read-write/>
< column name =Id/>
< generator class =assigned/>
< / id>
<! - snipp - >
< one-to-one cascade =deleteclass =Prayon.Entities.AppointmentRecurrence,Prayon.Entities,Version = 1.0.0.867,Culture = neutral,PublicKeyToken = nullforeign-key =FK_Activity_AppointmentRecurrence name =AppointmentRecurrence/>
< / class>
< / hibernate-mapping>


class xmlns =urn:nhibernate-mapping-2.2lazy =falsename =Prayon.Entities.AppointmentRecurrence,Prayon.Entities,Version = 1.0.0.867,Culture = neutral,PublicKeyToken = null表= AppointmentRecurrence >
< cache usage =read-write/>
< column name =Id/>
< generator class =foreign>
< param name =property>活动< / param>
< / generator>
< / id>
<! - snipp - >
< one-to-one class =Prayon.Entities.Activity,Prayon.Entities,Version = 1.0.0.867,Culture = neutral,PublicKeyToken = nullconstrained =trueforeign-key =FK_AppointmentRecurrence_Activity name =Activity/>
< / class>
< / hibernate-mapping>

现在,当我选择Activities(使用cachable-select)时,我在NHibernate- Profiler ,NHibernate正在从第二级缓存中获取每个Activity,但是它会为每个Activity选择一个AppointmentReccurrence。我能做什么,它也会从缓存中取得预约回复?
我试图在一对一的关系上设置一个缓存属性,但似乎不支持。

解决方案

你的映射在 Constrained 一边似乎有问题。根据Ayende的一对一指南:


另外需要注意的是,我们必须用foreign-key =none来指定,否则NHibernate的Schema Export功能会创建两个外键我们,这将创建一个循环的引用,不会允许我们插入任何东西到数据库中。

显然,FK不 存在于你的数据库中,但是错误的配置可能会导致缓存问题:



指定 foreign -key =none in Fluent:

  this.HasOne(x => x。 。活性).Constrained()ForeignKey的(); 


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.

解决方案

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

这篇关于一对一映射不适用于二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆