延迟加载NHibernate的与Ignore.NotFound加载、NHibernate、NotFound、Ignore

2023-09-04 02:52:34 作者:磨人小妖精

我有这样的波纹管的候选对象的映射:

I have a mapping like the bellow for a Candidate object:

References(x => x.Country).Column("CountryId").NotFound().Ignore()

这里的问题是,如果我选择*考生,我得到一个额外的选择为他们每个人的,不是一件好事,所以我拉出NOTFOUND()。忽略()位,但现在下code失败与ObjectNotFoundException异常:

the problem here is, if I select * Candidates I get a extra select for each of them, not a good thing, so I pull out the NotFound().Ignore() bit but now the following code fails with ObjectNotFoundException exception:

if (entity.Country != null)
{
       bos.CountryName = entity.Country.Name;
}

有没有办法强制Hhibernate做选择时,我比较县!= NULL?

Is there a way to force Hhibernate do the select when I compare County != null ?

感谢您,

推荐答案

在指定.NotFound()。忽略(),这迫使即时加载实体,并且不能与.LazyLoad重写()。 NHibernate的这样做,因为它必须确保关系的存在或不存在,因为你不依赖于数据库上执行此。

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.

我的建议是要么赶ObjectNotFoundException或修复您的数据,你没有这些不一致的地方。

My suggestion would be to either catch the ObjectNotFoundException or to fix your data such that you don't have these inconsistencies.

下面是关于这方面的文章: http://opensource.atlassian.com/项目/休眠/浏览/ HHH-2753

Here's an article about this: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2753

 
精彩推荐
图片推荐