强制惰性实体加载真实实例 [英] Force lazy entity to load real instance
问题描述
我有一个惰性实体的代理,该惰性实体是通过加载子实体在会话中创建的.对父实体的后续提取仅返回NH代理.我需要实际实例来检查类型(实体已加入子类).我一定想念一些东西,但是我找不到办法. Session.Refresh(proxy)似乎没有帮助,我尝试过的HQL也没有帮助.
I have a proxy for a lazy entity which has been created in the session by loading a child entity. A subsequent fetch on the parent entity only returns the NH proxy. I need the actual instance to check the type (the entity has joined subclasses). I must be missing something, but I can't find a way to do this. Session.Refresh(proxy) does not appear to help, nor does any flavour of HQL that I've tried.
任何人都可以帮忙吗?
推荐答案
要强制从数据库中获取代理,可以使用NHibernateUtil.Initialize(proxy)
方法,或访问代理的方法/属性.
To force a proxy to be fetched from the database, you can use the NHibernateUtil.Initialize(proxy)
method, or access a method/property of the proxy.
var foo = session.Get<Foo>(id);
NHibernateUtil.Initialize(foo.Bar);
要检查对象是否已初始化,可以使用NHibernateUtil.IsInitialized(proxy)
方法.
To check if an object is initialized or not, you can use the NHibernateUtil.IsInitialized(proxy)
method.
更新:
要从会话缓存中删除对象,请使用Session.Evict(obj)
方法.
To remove an object from the session cache, use the Session.Evict(obj)
method.
session.Evict(myEntity);
有关Evict
的信息和其他用于管理会话缓存的方法,可以在第14.5章.
Info about Evict
and other methods for managing the session cache can be found in chapter 14.5 of the NHibernate docs.
这篇关于强制惰性实体加载真实实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!