NHibernate,如何禁用对孤立引用的额外选择
NHibernate, how to disable additional selects for orphaned references
我正在使用 NHibernate 进行数据访问并遇到这种情况
public class B
{
public virtual long Id { get; set;}
...
}
public class B
{
public virtual long Id { get; set;}
public virtual A AReference { get; set; }
...
}
和映射
public class BMapping : ClassMapping<B>
{
...
ManyToOne(x => x.AReference, mapper =>
{
mapper.ForeignKey("none");
mapper.Insert(false);
mapper.Update(false);
mapper.Column("a_id");
mapper.Fetch(FetchKind.Join);
mapper.NotFound(NotFoundMode.Ignore);
});
...
}
我的问题是 tableA
和 tableB
之间没有外键约束,所以有时 tableB
引用不存在的项目。 (我知道那很糟糕,但对此无能为力).
设置
mapper.NotFound(NotFoundMode.Ignore);
允许我在没有 "No row with the given identifier exists" 异常的情况下检索数据,但似乎 NHibernate 正在尝试使用像
这样的单独选择来加载那些孤立的项目
SELECT ... FROM tableA where tableA.ID = ?
所以,问题是,我可以禁用额外的查询吗?如何禁用。
提前致谢。
这里讨论的NHibernate Prevent Lazy Loading of unmatched reference and here Lazy loading for NHibernate with Ignore.NotFound重点是:
When you specify the .NotFound().Ignore() this forces the entity to be eagerly loaded and cannot be overriden with the .LazyLoad(). NHibernate does this because it has to be sure that relationship exists or doesn't exist since you are not relying on the database to enforce this.
检查此
- NHibernate force not-found ignore to not execute an extra select
主要是那里提供的link
How to use 0 instead of null for foreign keys
了解如何创建要使用的自定义 PocoEntityTuplizer。
.. during the build process of the Person entity will collection object[] values contain also CountryProxy. Let's say that missing in DB is one with Id == 0 (use your own logic there as needed). This proxy will be replaced with null so no SELECT will be executed...
我正在使用 NHibernate 进行数据访问并遇到这种情况
public class B
{
public virtual long Id { get; set;}
...
}
public class B
{
public virtual long Id { get; set;}
public virtual A AReference { get; set; }
...
}
和映射
public class BMapping : ClassMapping<B>
{
...
ManyToOne(x => x.AReference, mapper =>
{
mapper.ForeignKey("none");
mapper.Insert(false);
mapper.Update(false);
mapper.Column("a_id");
mapper.Fetch(FetchKind.Join);
mapper.NotFound(NotFoundMode.Ignore);
});
...
}
我的问题是 tableA
和 tableB
之间没有外键约束,所以有时 tableB
引用不存在的项目。 (我知道那很糟糕,但对此无能为力).
设置
mapper.NotFound(NotFoundMode.Ignore);
允许我在没有 "No row with the given identifier exists" 异常的情况下检索数据,但似乎 NHibernate 正在尝试使用像
这样的单独选择来加载那些孤立的项目SELECT ... FROM tableA where tableA.ID = ?
所以,问题是,我可以禁用额外的查询吗?如何禁用。
提前致谢。
这里讨论的NHibernate Prevent Lazy Loading of unmatched reference and here Lazy loading for NHibernate with Ignore.NotFound重点是:
When you specify the .NotFound().Ignore() this forces the entity to be eagerly loaded and cannot be overriden with the .LazyLoad(). NHibernate does this because it has to be sure that relationship exists or doesn't exist since you are not relying on the database to enforce this.
检查此
- NHibernate force not-found ignore to not execute an extra select
主要是那里提供的link
How to use 0 instead of null for foreign keys
了解如何创建要使用的自定义 PocoEntityTuplizer。
.. during the build process of the Person entity will collection object[] values contain also CountryProxy. Let's say that missing in DB is one with Id == 0 (use your own logic there as needed). This proxy will be replaced with null so no SELECT will be executed...