hibernate 里错误:no session or session was closed

集合延迟加载了,把lazy改成false,或者使用OpenSessionInView

把mapping文件里面的set那部分的lazy设置为false

既然filter能够open、close Session,当然你也可以啊,看看filter里怎么写的,照猫画虎不就知道啦:D现在只是你不能指望在init方法里filter给你管理session,你必须手动的打开一个session,用完以后close。这样是代价最小的修改,其他代码不需要改。

 

 /// <summary>
        /// 延迟加载
        /// </summary>
        public static void TestLazyLoad()
        {
            //[HasMany(typeof(Post), Table="posts", ColumnKey="post_blogid",Lazy=true)]
            //注: 请先设置Lazy=true,来启用延迟加载
            using (new SessionScope())
            {//ActiveRecord在实现延迟载入时必须使用new SessionScope()
                Blog blog = Blog.Find(1);
                string sResultname = blog.Name;
                int iresultCount = blog.Posts.Count;
                IList ilist = blog.Posts;

                Console.WriteLine(resultCount.ToString());
            }
        }

posted on 2012-11-20 11:02  一个石头  阅读(172)  评论(0)    收藏  举报