Nhibernate 中具有复合 ID 的意外代理对象

发布于 2024-07-11 19:53:26 字数 1291 浏览 8 评论 0原文

我有一个使用复合 id 的数据结构(我不想更改为单个) 除了多对一连接之外,所有内容都加载良好,如果连接为空,则不会将属性映射到 null,而是将其映射到空代理对象。 我写了一个丑陋的解决方案(见下文)。 有什么解决办法吗?

私有节点_Parent;

    public Node Parent
    {
        get
        {
            return this._Parent;
        }
        set
        {
            this._Parent = Proxy.Check<Node>(value);
        }
    }
internal static class Proxy
{
    public static T Check<T>(T obj) where T : PersistentObject
    {
        if (obj is NHibernate.Proxy.INHibernateProxy && obj != null)
        {
            try 
            {
                int id = obj.ID;
                return obj;             
            }
            catch //Proxy only object cant retrieve ID
            {
                return null;
            }
        }
        else
        {
            return obj;
        }
    }

}

以映射文件开头

<class name="Node" table="Node">
    <composite-id>
        <key-property name="ID"/>
        <key-property name="VersionID"/>
    </composite-id>

并通过访问

    <many-to-one name="Node" class="Node" >
        <column name="NodeID"/>
        <column name="VersionID" />
    </many-to-one>

I have a data structure which uses composite ids (Which I dont wish to change to single)
Everything loads fine except for many-to-one joins which if the join is empty, instead of mapping the property to null, maps it to an empty proxy object. I have written an ugly work around( see bleow). Any solutions to this?

private Node _Parent;

    public Node Parent
    {
        get
        {
            return this._Parent;
        }
        set
        {
            this._Parent = Proxy.Check<Node>(value);
        }
    }
internal static class Proxy
{
    public static T Check<T>(T obj) where T : PersistentObject
    {
        if (obj is NHibernate.Proxy.INHibernateProxy && obj != null)
        {
            try 
            {
                int id = obj.ID;
                return obj;             
            }
            catch //Proxy only object cant retrieve ID
            {
                return null;
            }
        }
        else
        {
            return obj;
        }
    }

}

with the mapping file beginning

<class name="Node" table="Node">
    <composite-id>
        <key-property name="ID"/>
        <key-property name="VersionID"/>
    </composite-id>

and accessed by

    <many-to-one name="Node" class="Node" >
        <column name="NodeID"/>
        <column name="VersionID" />
    </many-to-one>

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

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

发布评论

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

评论(1

杀お生予夺 2024-07-18 19:53:26

不确定这是否是这种情况的完美解决方案,但是当我在使用复合键处理旧数据库时遇到同样的问题时,这解决了我的问题。

通过将 not-found 设置为忽略链接,NHibernate 会将空对象视为 null 而不是异常。 当使用这种技术时,NHibernate 将执行一个单独的查询,因此可能会有小的性能影响,因为这基本上是预先加载对象。

您可以尝试只是急切地加载对象而不是使用此技术,但我有一种感觉它会返回异常,因为它需要一个对象(不为空)。 如果这不起作用,我建议在 NHibernate 论坛中发布一个问题,因为我绝对不是该领域的专家,但这对您来说可能是一个更小/不那么丑陋的解决方案。

例如:

<many-to-one name="Node" class="Node" not-found="ignore">
    <column name="NodeID"/>
    <column name="VersionID" />
</many-to-one>

希望这有帮助,

杰伊

Not exactly sure if this is the perfect fix for this situation, but this fixed the issue for me when I encountered the same problem while working on an old DB with composite keys.

By setting not-found to ignore on your links, NHibernate will treat empty objects as null instead of exceptions. When using this technique NHibernate will execute a seperate query so there may be small performance hits, as this is basically eager loading the object.

You could try just eager loading the object instead of using this technique, but I have a feeling it would return an exception as it would be expecting an object (not null). I would suggest posting a question in the NHibernate forums if this doesn't work as I am definately not an expert in this area, but this may be a smaller/less ugly work around for you.

For example:

<many-to-one name="Node" class="Node" not-found="ignore">
    <column name="NodeID"/>
    <column name="VersionID" />
</many-to-one>

Hope this helps,

Jay

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