如何在另一个会话中更新具有惰性属性(代理对象)的对象?
How to update object with lazy properties (proxy object) in another session?
我试图在一个会话中获取具有惰性属性的对象,并试图在另一个会话中更新它。但它没有这样做
出现 错误:SecUserProxy 没有持久性(实际 class 是 SecUser)
我正在使用 NHibernate 3.4。当我用谷歌搜索时,我开始知道它的 bug 已经被修复了。
我也遇到了这个post,其中说如果你的代理对象实现了 INhibernateProxy,你可以取消代理该对象
与 NHibernate。由于 NHibernate 不再支持可插入代理工厂(如 Castle、LinFu 等),它使用 internal 一个,我是
假设 internal 一个可能是 INhibernateProxy
所以我在新会话中执行了以下操作,我想将我的对象更新为:
object unprox_obj = Session
.GetSessionImplementation()
.PersistenceContext.Unproxy(secUserobj);
期待获得相同的对象,但具有真实类型,即 SecUser,以便它可以无错误地更新。但它仍然是 returns 一个代理对象。
我无法理解发生了什么?
更新:
我刚刚意识到 'secUserobj' 不是 INhibernateProxy。那么我怎样才能使它成为 INhibernateProxy 以便在另一个会话中更新我的对象?
if (secUserobj is INHibernateProxy)
{
unprox_obj = Session
.GetSessionImplementation()
.PersistenceContext.Unproxy(secUserobj);
}
分离的对象(在一个会话中加载,并保留作为参考) 可以重新附加。我们可以使用 session.Merge()
或 session.Lock()
9.4.2. Updating detached objects
Many applications need to retrieve an object in one transaction, send it to the UI layer for manipulation, then save the changes in a new transaction....
...
...The last case can be avoided by using Merge(Object o)
. This method copies 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. The method returns the persistent instance. If the given instance is unsaved or does not exist in the database, NHibernate will save it and return it as a newly persistent instance. Otherwise, the given instance does not become associated with the session. In most applications with detached objects, you need both methods, SaveOrUpdate()
and Merge()
.
19.1.4. Initializing collections and proxies
...
You may also attach a previously loaded object to a new ISession with Merge()
or Lock()
before accessing uninitialized collections (or other proxies). No, NHibernate does not, and certainly should not do this automatically, since it would introduce ad hoc transaction semantics!
...
因此,我们可以将分离引用传递给 .Merge()
,然后使用返回的 (全新) 对象引用:
MyEntity reAttached = session.Merge<MyEntity>(detached);
小心,这应该在接触任何分离的集合之前完成(如上所述)。
我试图在一个会话中获取具有惰性属性的对象,并试图在另一个会话中更新它。但它没有这样做 出现 错误:SecUserProxy 没有持久性(实际 class 是 SecUser)
我正在使用 NHibernate 3.4。当我用谷歌搜索时,我开始知道它的 bug 已经被修复了。
我也遇到了这个post,其中说如果你的代理对象实现了 INhibernateProxy,你可以取消代理该对象 与 NHibernate。由于 NHibernate 不再支持可插入代理工厂(如 Castle、LinFu 等),它使用 internal 一个,我是 假设 internal 一个可能是 INhibernateProxy
所以我在新会话中执行了以下操作,我想将我的对象更新为:
object unprox_obj = Session
.GetSessionImplementation()
.PersistenceContext.Unproxy(secUserobj);
期待获得相同的对象,但具有真实类型,即 SecUser,以便它可以无错误地更新。但它仍然是 returns 一个代理对象。
我无法理解发生了什么?
更新: 我刚刚意识到 'secUserobj' 不是 INhibernateProxy。那么我怎样才能使它成为 INhibernateProxy 以便在另一个会话中更新我的对象?
if (secUserobj is INHibernateProxy)
{
unprox_obj = Session
.GetSessionImplementation()
.PersistenceContext.Unproxy(secUserobj);
}
分离的对象(在一个会话中加载,并保留作为参考) 可以重新附加。我们可以使用 session.Merge()
或 session.Lock()
9.4.2. Updating detached objects
Many applications need to retrieve an object in one transaction, send it to the UI layer for manipulation, then save the changes in a new transaction....
...
...The last case can be avoided by using
Merge(Object o)
. This method copies 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. The method returns the persistent instance. If the given instance is unsaved or does not exist in the database, NHibernate will save it and return it as a newly persistent instance. Otherwise, the given instance does not become associated with the session. In most applications with detached objects, you need both methods,SaveOrUpdate()
andMerge()
.
19.1.4. Initializing collections and proxies
...
You may also attach a previously loaded object to a new ISession withMerge()
orLock()
before accessing uninitialized collections (or other proxies). No, NHibernate does not, and certainly should not do this automatically, since it would introduce ad hoc transaction semantics!
...
因此,我们可以将分离引用传递给 .Merge()
,然后使用返回的 (全新) 对象引用:
MyEntity reAttached = session.Merge<MyEntity>(detached);
小心,这应该在接触任何分离的集合之前完成(如上所述)。