NHibernate - 多对一 - NHibernate.LazyInitializationException - 无法初始化代理
当我尝试访问存储在域对象属性中的对象时,出现此异常。我做了一些研究,但仍然不明白为什么会出现此错误。
我有一个非常基本的存储库,在其中创建一个会话,然后使用 ICriteria 查询从结果列表中获取第一条记录。我的连接域对象具有映射到服务器的一对多关系。为什么会话在获取 Connection 对象时不包含服务器作为代理?我不太熟悉 NHibernate 的会话管理。
这是我的实现:
Domain obj:
public class Connection {
public virtual int Id { get; private set; }
public virtual string FullName { get; set; }
public virtual string Email { get; set; }
public virtual string NickName { get; set; }
public virtual string Alternative { get; set; }
public virtual bool IsInvisible { get; set; }
public virtual Server CurrentServer { get; set; }
}
public Connection GetConnection()
{
using (ISession session = NHibernateHelper.OpenSession())
{
ICriteria crit = session.CreateCriteria(typeof(Connection));
crit.SetMaxResults(1);
IList<Connection> connection = crit.List<Connection>();
return connection[0];
}
}
上面将成功返回一个 Connection 对象。但是,访问 CurrentServer 属性会引发异常。我的假设是 NHibernate 知道该对象与“CurrentServer”的关系,因此会在请求时延迟加载该对象。有人能告诉我我在哪里被引入歧途吗?
谢谢!
I am getting this exception when I attempt to access an object that is stored in a property of my domain object. I have done some research and still do not understand why I am getting this error.
I have a very basic repository in which I am creating a session and then using a ICriteria query to fetch the first record from the list of results. My Connection domain object has one to many relationship mapped to Server. Why does the session not include the server as a proxy when it fetches the Connection object? I'm not very familiar session managment with NHibernate.
Here is my implementation:
Domain obj:
public class Connection {
public virtual int Id { get; private set; }
public virtual string FullName { get; set; }
public virtual string Email { get; set; }
public virtual string NickName { get; set; }
public virtual string Alternative { get; set; }
public virtual bool IsInvisible { get; set; }
public virtual Server CurrentServer { get; set; }
}
public Connection GetConnection()
{
using (ISession session = NHibernateHelper.OpenSession())
{
ICriteria crit = session.CreateCriteria(typeof(Connection));
crit.SetMaxResults(1);
IList<Connection> connection = crit.List<Connection>();
return connection[0];
}
}
The above will successfully return a Connection object. However accessing the CurrentServer property with throw the exception. It is/was my assumption that NHibernate was aware of the relationship that this object has with 'CurrentServer' and would therefore load that object, lazily, when requested. Can someone tell me where I was led astray?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果是 Web 应用程序,请使用每个请求会话模式。如果它是 Windows 应用程序。这将需要更多的工作。我在 Windows 窗体的生命周期内保持一个打开的会话,并将其处理在 FormClosing 事件中。除了少数情况外,这对所有情况都很有效。
为了15分我不会做什么? :-)
If it's a web app., use the session-per-request pattern. If it's a Windows app. it will take more work. I hold a session open for the lifetime of a Windows Form and dispose it in the FormClosing event. This works great for all but a few cases.
What won't I do for 15 points? :-)
您首先处理会话,然后开始延迟加载。对象应连接到会话以启用延迟加载。
You dispose the session first, and start the lazy loading after that. An object should be connected to a session to enable lazy loading.
由于 Jamie Ide 的评论,我找到了答案。问题是我将会话包装在 using 语句中。这意味着当我尝试访问服务器属性时,当前会话已被释放,因此延迟加载无法使用该会话来获取服务器。
I found the answer due to Jamie Ide's comment. The problem was that I was wrapping my session in a using statement. That meant that when I attempted to access the Server property the current session had already been disposed and therefore the lazy loading could not use that session to fetch the server.