ISession.SaveOrUpdateCopy() 和 ISession.Merge() 有什么区别?

发布于 2024-11-28 01:56:49 字数 633 浏览 2 评论 0原文

在 NHibernate 3.1 中,ISession.SaveOrUpdateCopy() 已被标记为已弃用。该文档建议使用 Merge() 代替。每个的文档如下:

SaveOrUpdateCopy(object obj)

将给定对象的状态复制到具有相同标识符的持久对象上。如果当前没有关联的持久实例 会话,它将被加载。返回持久化实例。如果 给定实例未保存或数据库中不存在,保存它并 将其作为新的持久实例返回。否则,给定的实例 不会与会话关联。

合并(对象obj)

将给定对象的状态复制到具有相同状态的持久对象上 标识符。如果当前没有关联的持久实例 会话,它将被加载。返回持久化实例。如果 给定实例未保存,保存副本并将其作为新持久的返回 实例。给定实例不会与会话关联。 如果关联已映射,则此操作会级联到关联实例 与 cascade="merge"
该方法的语义由 JSR-220 定义。

它们在我看来几乎一模一样,但肯定存在一些微妙之处。如果有,它们是什么?

In NHibernate 3.1, ISession.SaveOrUpdateCopy() has been marked as deprecated. The documentation suggests using Merge() instead. The documentation for each is as follows:

SaveOrUpdateCopy(object obj)

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with
the session, it will be loaded. Return the persistent instance. If the
given instance is unsaved or does not exist in the database, save it and
return it as a newly persistent instance. Otherwise, the given instance
does not become associated with the session.

Merge(object obj)

Copy the state of the given object onto the persistent object with the same
identifier. If there is no persistent instance currently associated with
the session, it will be loaded. Return the persistent instance. If the
given instance is unsaved, save a copy of and return it as a newly persistent
instance. The given instance does not become associated with the session.
This operation cascades to associated instances if the association is mapped
with cascade="merge".
The semantics of this method are defined by JSR-220.

They look nearly identical to me, but there are bound to be some subtleties involved. If so, what are they?

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

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

发布评论

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

评论(1

剑心龙吟 2024-12-05 01:56:49

SaveOrUpdateCopy 现在被认为已过时,因此 Merge 旨在取代它(因此它极其相似)。

它们几乎是相同的,只是我不认为这些级联选项可用于 SaveOrUpdateCopy。然而,这一点没有实际意义,因为合并应该是您使用的方法。


更新:我查看了 NHibernate 的源代码,只是为了确保它们与我想象的一样相似,这就是我发现的内容。

MergeSaveOrUpdateCopy 都有非常相似的实现:

public object Merge(string entityName, object obj)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        return FireMerge(new MergeEvent(entityName, obj, this));
    }
}

public object SaveOrUpdateCopy(object obj)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        return FireSaveOrUpdateCopy(new MergeEvent(null, obj, this));
    }
}

它们的 FireXXXX 方法也非常相似:

private object FireMerge(MergeEvent @event)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        CheckAndUpdateSessionStatus();
        IMergeEventListener[] mergeEventListener = listeners.MergeEventListeners;
        for (int i = 0; i < mergeEventListener.Length; i++)
        {
            mergeEventListener[i].OnMerge(@event);
        }
        return @event.Result;
    }
}

private object FireSaveOrUpdateCopy(MergeEvent @event)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        CheckAndUpdateSessionStatus();
        IMergeEventListener[] saveOrUpdateCopyEventListener = listeners.SaveOrUpdateCopyEventListeners;
        for (int i = 0; i < saveOrUpdateCopyEventListener.Length; i++)
        {
            saveOrUpdateCopyEventListener[i].OnMerge(@event);
        }
        return @event.Result;
    }
}

除了利用不同的事件之外,方法完全相同监听器列表,但列表的类型 (IMergeEventListener) 也是相同的!

查看侦听器列表,它们都是使用默认侦听器进行初始化的。 Merge 侦听处理程序的默认侦听器的类型为 DefaultMergeEventListener,而 SaveOrUpdateCopy 的类型为 DefaultSaveOrUpdateCopyEventListener。因此,它们之间的区别只是这两种实现的区别(也就是说,如果您保留默认侦听器,这是 99% 的时间)。

然而,真正有趣的事实是实现上的差异。如果您查看 DefaultSaveOrUpdateCopyEventListener,您会得到以下结果:

public class DefaultSaveOrUpdateCopyEventListener : DefaultMergeEventListener
{
    protected override CascadingAction CascadeAction
    {
        get { return CascadingAction.SaveUpdateCopy; }
    }
}

这意味着 MergeSaveOrUpdateCopy 的默认行为仅在级联操作中有所不同,其他所有内容都完全相同相同。

SaveOrUpdateCopy is now considered obsolete and thus Merge is meant to take over for it (hence its extreme similarity).

They are pretty much the same except I don't think those cascade options were available with SaveOrUpdateCopy. However, that point is moot as Merge should be method you use.


UPDATE: I went in to the source code of NHibernate just to make sure they are as similar as I was thinking and here is what I found.

Both Merge and SaveOrUpdateCopy have very similar implementations:

public object Merge(string entityName, object obj)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        return FireMerge(new MergeEvent(entityName, obj, this));
    }
}

public object SaveOrUpdateCopy(object obj)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        return FireSaveOrUpdateCopy(new MergeEvent(null, obj, this));
    }
}

Their FireXXXX methods are also very similar:

private object FireMerge(MergeEvent @event)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        CheckAndUpdateSessionStatus();
        IMergeEventListener[] mergeEventListener = listeners.MergeEventListeners;
        for (int i = 0; i < mergeEventListener.Length; i++)
        {
            mergeEventListener[i].OnMerge(@event);
        }
        return @event.Result;
    }
}

private object FireSaveOrUpdateCopy(MergeEvent @event)
{
    using (new SessionIdLoggingContext(SessionId))
    {
        CheckAndUpdateSessionStatus();
        IMergeEventListener[] saveOrUpdateCopyEventListener = listeners.SaveOrUpdateCopyEventListeners;
        for (int i = 0; i < saveOrUpdateCopyEventListener.Length; i++)
        {
            saveOrUpdateCopyEventListener[i].OnMerge(@event);
        }
        return @event.Result;
    }
}

The methods are exactly the same except they draw on different event listener lists, but even the types of the lists (IMergeEventListener) are the same!

Looking at the listener lists, they are both initialized with a default listener. The default listener for the Merge listen handlers is of type DefaultMergeEventListener while the SaveOrUpdateCopy is DefaultSaveOrUpdateCopyEventListener. Thus, the difference between them is just the difference in these two implementations (that is if you keep the default listener, which is 99% of the time).

However, the real interesting fact IS the difference in implementation. If you look at DefaultSaveOrUpdateCopyEventListener you get this:

public class DefaultSaveOrUpdateCopyEventListener : DefaultMergeEventListener
{
    protected override CascadingAction CascadeAction
    {
        get { return CascadingAction.SaveUpdateCopy; }
    }
}

This means the default behavior for Merge and SaveOrUpdateCopy only differs in the cascading actions, everything else is exactly the same.

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