NHibernate强制找不到忽略不执行额外的选择 [英] NHibernate force not-found ignore to not execute an extra select

查看:108
本文介绍了NHibernate强制找不到忽略不执行额外的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个旧数据库,该数据库并不总是使用FK. 例如,我有一个实体人"和一个实体国家".一个人的国家/地区在人"映射中被映射为多对一.

I'm working with a legacy database where FK's aren't always used. For example I have an entity Person and an entity Country. A person has a Country which is mapped as a many-to-one in the Person mapping.

<many-to-one name="Country" class="Country" foreign-key="none" lazy="false" not-found="ignore" fetch="join" outer-join="true" column="countryid"/>

当某人具有null作为列值(国家/地区ID)时,它不会执行额外的选择查询(因为它知道国家/地区表中将没有引用),但是当某人具有0时因为列值NH将执行另一个选择以检查国家/地区表(如果该国家/地区实际上不存在).但是因为我们进行了左外部连接,所以NH应该已经知道它不存在. 只是为了澄清一下,如果该列的值是1并且它存在于国家/地区表中,则它不会执行额外的选择.

When person has null as column value (countryid) it won't perform an extra select query (because it knows that there won't be a reference in the country table), but when a person has 0 as column value NH will perform another select to check the country tabel wether the country actually doesn't exist. But because we do a left outer join, NH should already know that it doesn't exist. Just to clarify, if a the column has a value of 1 and it is present in the country table, it will not perform an extra select.

反正有什么要告诉NHibernate不要做额外的选择查询吗?

Is there anyway to tell NHibernate not to do the extra select query?

谢谢

推荐答案

否,无法通过配置执行此操作.不错的帖子,介绍如何改善 not-found="ignore"功能:

No, there is no way how to do this via configuration. Nice post how to improve the not-found="ignore" functionality:

http://nhforge.org/blogs/nhibernate/archive/2011/01/28/how-to-use-0-instead-of-null-for-foreign-keys.aspx

我们可以使用一些NHibernate扩展点,例如自定义PocoEntityTuplizer,但是没有简单的配置,没有设置...

We can use some of NHibernate extension points like custom PocoEntityTuplizer, but there is no simple configuration, no setting...

某些摘录: 从上方的链接(阅读以获取更多详细信息).在Person实体的构建过程中,集合object[] values还将包含CountryProxy.假设在数据库中缺少的是Id == 0的一个(需要时使用您自己的逻辑).此代理将替换为null,因此不会执行任何SELECT ...

Some extract: from the link above (read it to get more details). 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...

public class NullableTuplizer : PocoEntityTuplizer
{
    public override void SetPropertyValues(object entity, object[] values)
    {
        for (int i = 0; i < values.Length; i++)
        {
            if (typeof (Country).IsAssignableFrom(getters[i ].ReturnType)
                && ((Country) values[i]).Id == 0) // missing id 
            {
                values[i] = null; // here change a Proxy to null
            }
        }
        base.SetPropertyValues(entity, values);
    }
...

这篇关于NHibernate强制找不到忽略不执行额外的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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