NHibernate.LazyInitializationException [英] NHibernate.LazyInitializationException

查看:523
本文介绍了NHibernate.LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直有这个问题,弹出零星的,但现在我可以每一次重现。我递增在我定制的论坛上查看计数器,它导致错误:


  

NHibernate.LazyInitializationException:无法初始化懒洋洋地收集,没有会话或会话关闭


在对象另一个集合时出现此错误。如果我补充一下:

.Not.LazyLoad()

要我流利的映射,在我的项目错误变化。我一直intil它的对象会在没有延迟加载现货禁用延迟加载,然后扔了这个错误:


  

NHibernate.LazyInitializationException:无法初始化代理 - 没有会话


所以,后来我拿出穷人对我的懒加载,现在我又回到了起点。它只有当我增加这种观点计数器错误。这里是我的基类的一个片段节省​​code:

 使用(ISession的会话=的getSession())
使用(ITransaction TX = session.BeginTransaction())
{
       session.SaveOrUpdate(实体);
       tx.Commit();
}

环顾四周,我在另一篇文章,该交易可能导致的问题看,但那是因为他们被安置在那里。这code扩展到那些从我的域对象(库类)的单独的类。下面是帖子:

<一个href=\"http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy\">http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

我不相信这是我的问题在这里。这里是我所引发错误的第一个集合流利的映射。还有其他几个类似的集合。

  HasManyToMany(X =&GT; x.Votes)
    .WithTableName(PostVotes)
    .WithParentKeyColumn(帖子ID)
    .WithChildKeyColumn(VoteId);


解决方案

一个小进一步研究这个问题,因为它已经重现本身就是在单个回调到服务器,如果你做一个保存和获取,你需要刷新会话。我做我的冲洗后保存这似乎已经纠正了这个问题。

We have been having this issue pop up sporadically, but now I can reproduce it every time. I am incrementing a view counter on my custom built forums, which causes an error:

NHibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed

This error occurs on another collection in the object. If I add:

.Not.LazyLoad()

To my Fluent mapping, the error shifts around my project. I kept disabling lazy loading on objects intil it going to a spot where there was no lazy loading, and then it threw this error:

NHibernate.LazyInitializationException: Could not initialize proxy - no Session.

So, then I took out the nots on my lazy loading and now I'm back to square one. It only errors when I increment this view counter. Here is a snippet of my base class save code:

using (ISession session = GetSession())
using (ITransaction tx = session.BeginTransaction())
{
       session.SaveOrUpdate(entity);
       tx.Commit();
}

Looking around, I read in another post that the transactions can cause an issue, but that was because of where they were placed. This code is extended to classes that are separate from my domain objects (repository classes). Here is the post:

http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

I don't believe that is my issue here. Here is my fluent mapping for the first collection that is throwing the error. There are several other similar collections.

HasManyToMany(x => x.Votes)
    .WithTableName("PostVotes")
    .WithParentKeyColumn("PostId")
    .WithChildKeyColumn("VoteId");

解决方案

A little further research into this issue as it has reproduced itself is that in a single call back to the server, if you do a save and get, you need to flush the session. I do my flush after the save and that seems to have corrected the problem.

这篇关于NHibernate.LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆