我已经使用NHibernate3声明了fallowing映射:
使用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-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>
现在,当我选择Activities(带有cachable-select)时,我在NHibernate-Profiler中看到,NHibernate正在使每个Activity更正出二级缓存,但它会为每个Activity做一个选择到约会重生。我能做什么,它还需要来自缓存的AppointmentReccurrence? 我试图在一对一关系上设置缓存属性,但似乎不支持。
答案 0 :(得分:0)
您的映射的Constrained
方似乎存在问题。根据Ayende的one-to-one指南:
需要注意的是我们必须使用foreign-key =“none”来指定它,因为否则NHibernate的Schema Export功能会为我们创建两个外键,这将创建一个不允许我们插入的循环引用任何东西进入数据库。
显然,FK实际上 存在于您的数据库中,但错误配置可能会导致缓存出现问题:
在Fluent中指定foreign-key="none"
的方法:
this.HasOne(x => x.Activity).Constrained().ForeignKey();