nhibernate,外键列中为 0

发布于 2024-10-14 07:48:53 字数 182 浏览 5 评论 0原文

我有一个遗留数据库,其中 FK 列中使用值 0 来指示未指定任何关系。

这不是我可以通过微不足道的方式改变的。是否可以告诉 NHibernate 将指定列中的 0 视为 null?

编辑

我知道not-found,但我只想忽略那些带有0的。

I got a legacy database where the value of 0 was used in FK columns to indicate that no relation have been specified.

This is not something that I can change in a trivial way. Is it possible to tell NHibernate to treat 0 as null in specified columns?

Edit

I know about not-found, but I just want to ignore those with 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-10-21 07:48:53

这个解决方案对我们来说非常有用:http://nhibernate.info/blog/2011/01/28/how-to-use-0-instead-of-null-for-foreign-keys.html

简而言之:

添加以下类:

public class NullableTuplizer : PocoEntityTuplizer
{
    public NullableTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity)
        : base(entityMetamodel, mappedEntity)
    {
    }

    public override object[] GetPropertyValuesToInsert(
        object entity, IDictionary mergeMap, ISessionImplementor session)
    {
        object[] values = base.GetPropertyValuesToInsert(entity, mergeMap, session);
        //dirty hack 1
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i ] == null && typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType))
            {
                values[i ] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }

    public override object[] GetPropertyValues(object entity)
    {
        object[] values = base.GetPropertyValues(entity);
        //dirty hack 2
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i ] == null && typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType))
            {
                values[i ] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }


    public override void SetPropertyValues(object entity, object[] values)
    {
        //dirty hack 3.
        for (int i = 0; i < values.Length; i++)
        {
            if (typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType)
                && ((IEntity) values[i ]).Id == 0)
            {
                values[i ] = null;
            }
        }
        base.SetPropertyValues(entity, values);
    }
}

然后为每个相关映射注册它:

foreach (var persistentClass in configuration.ClassMappings)
{
    persistentClass.AddTuplizer(EntityMode.Poco, typeof(NullableTuplizer).AssemblyQualifiedName);
}

This solution worked great for us: http://nhibernate.info/blog/2011/01/28/how-to-use-0-instead-of-null-for-foreign-keys.html

In short:

Add the following class:

public class NullableTuplizer : PocoEntityTuplizer
{
    public NullableTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity)
        : base(entityMetamodel, mappedEntity)
    {
    }

    public override object[] GetPropertyValuesToInsert(
        object entity, IDictionary mergeMap, ISessionImplementor session)
    {
        object[] values = base.GetPropertyValuesToInsert(entity, mergeMap, session);
        //dirty hack 1
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i ] == null && typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType))
            {
                values[i ] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }

    public override object[] GetPropertyValues(object entity)
    {
        object[] values = base.GetPropertyValues(entity);
        //dirty hack 2
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i ] == null && typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType))
            {
                values[i ] = ProxyFactory.GetProxy(0, null);
            }
        }
        return values;
    }


    public override void SetPropertyValues(object entity, object[] values)
    {
        //dirty hack 3.
        for (int i = 0; i < values.Length; i++)
        {
            if (typeof (IEntity).IsAssignableFrom(getters[i ].ReturnType)
                && ((IEntity) values[i ]).Id == 0)
            {
                values[i ] = null;
            }
        }
        base.SetPropertyValues(entity, values);
    }
}

Then register it for every relevant mapping:

foreach (var persistentClass in configuration.ClassMappings)
{
    persistentClass.AddTuplizer(EntityMode.Poco, typeof(NullableTuplizer).AssemblyQualifiedName);
}
七月上 2024-10-21 07:48:53

还有一个类似的问题: NHibernate Saving 0 to多对一列而不是 null。诺埃尔·肯尼迪提出了一个有趣但有点奇怪的解决方案。

我可能会在数据库中存储一条 ID 为 0 的记录,它代表一个空对象。

There is a similar question: NHibernate Saving 0 to many-to-one column instead of null. There is an interesting, but a bit weird solution by Noel Kennedy.

I would probably store a record with the ID 0 in the database, which represents a Null Object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文