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

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

问题描述

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

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"/>

当 person 将 null 作为列值 (countryid) 时,它不会执行额外的选择查询(因为它知道国家表中不会有引用),但是当 a人有 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.假设 DB 中缺少的是 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 force not-found 忽略不执行额外的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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