LazyInitializationException: could not initialize proxy no session

这完全是框架设计者的锅,讲道理  无论是SSH SSM都太重了,

Hibernate几乎把SQL完全封装了一遍,简单的一对多关系,如果开启LazyLoad 这样实体类会被代理,直到访问这个多方实体的属性的时候,才会加载

在webxml中加入

<filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
           <init-param>
           <!-- 指定org.springframework.orm.jpa.LocalEntityManagerFactoryBean在spring配置文件中的名称,默认值为entityManagerFactory
           如果LocalEntityManagerFactoryBean在spring中的名称不是entityManagerFactory,该参数一定要指定,否则会出现找不到entityManagerFactory的例外 -->
            <param-name>entityManagerFactoryBeanName</param-name>
            <param-value>entityManagerFactory</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,可以上网搜索,

原理就是在Service层完成数据处理之后,View层来访问实体类属性的时候,Session不会关闭,直到view渲染完成才关闭Session,这样就不会出现 no session 的提示了

				
时间: 2024-08-07 21:19:00

LazyInitializationException: could not initialize proxy no session的相关文章

懒加载异常org.hibernate.LazyInitializationException: could not initialize proxy - no Session

错误页面提示 could not initialize proxy - no Session 控制台 org.hibernate.LazyInitializationException: could not initialize proxy - no Session 病症:这是一个lazy使用后的Exception,使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常. Q

懒加载的解决方法(全)org.hibernate.LazyInitializationException: could not initialize proxy - no Session

如下错误:org.hibernate.LazyInitializationException: could not initialize proxy - no Session 原因是懒加载的问题,因为hibernate的机制是当我们查询一个对象的时候,在默认情况下,返回的只是该对象的普通属性,当用户去使用对象属性的时候,才会向数据库再一次查询,可以这时session已经关闭了,无法对数据库进行查询. 举例:在界面显示雇员所在的部门名称${loginuser.department.name } 解

【bug记录】jpa 解决org.hibernate.lazyinitializationexception could not initialize proxy - no session

前言 最近开发项目比较忙,springcloud的笔记得稍稍放放了,下午出来个bug,恶心的不行,功能很简单,也没有什么级联或复杂的映射关系,就是一直在报三个异常 Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no propert

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

在<many-to-on>中这错误很常见,字面意义就是不能被初始化,因为session已经关闭了. 简单理解就是因为,你使用了lazy=true,这样hibernate在从数据库中调数据的时候是不会把关联的对象查出来的,而是保存一个获取值得方法,在你使用getXXX()调用的时候,hiberante会利用这个保存的方法去从数据库中取数据.而往往我们在jsp页面中使用getXXX()准备展示数据的时候,session早已经在dao中就关闭了,我们该如何解决这种异常呢? 简单的说有三种:1.把la

hibernate4.x lazy exception org.hibernate.LazyInitializationException: could not initialize proxy - no Session

在stackoverflow 上面找到一篇介绍 使用了 hibernate.enable_lazy_load_no_trans 的处理,配置后解决了错误: <bean id="sessionFactory"         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">         <property name="dataSource"

hibernate4 could not initialize proxy - no Session

相关代码链接: http://git.oschina.net/alexgaoyh/alexgaoyh 上周在编写hibernate4的相关代码的时候,出现异常 org.hibernate.LazyInitializationException: could not initialize proxy - no Session  网上出现最多的解决方案是  openSessionInViewFilter 形如: <!-- open session filter --> <filter>

ssh整合案例出了could not initialize proxy - no Session 问题,那个大神帮我看看

在做SSH项目时,控制台出现这个错误,请大神帮忙看看啊2017-04-14 11:15:57,902 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:1140) - Added autowiring by name from bean name 'cn.bdqn.jboa.action.EmployeeAction' via property 'employeeBiz' to bean nam

org.hibernate.LazyInitializationException: could not initialize proxy [com.angus.entity.ProductCateg

错误现象: org.hibernate.LazyInitializationException: could not initialize proxy [com.angus.entity.ProductCategory#1] - no Session 解决方式 在出问题的实体类上加@Proxy(lazy = false) 原因 hibernate默认是懒加载的. 原文地址:https://www.cnblogs.com/bestjdg/p/12105100.html

NHibernate之&quot;Could not initialize proxy - no Session&quot; Bug

最近在看一个 叫 泰斗破坏神的项目 里面用到了 NHibernate 这里涉及到了 联表查询 我的映射是在类里面,和网上在XML里的不一样 当我运行时,它会报错  Could not initialize proxy - no Session 网上查了的意思是  数据还没找完 Session就已经销毁了 映射表默认的是lazy=true;就是延迟加载的意思,因为延迟加载导致Session销毁了,然后我在想能不能 找到一个修改lazy的方法,可是最后找不到. 所以最后想到一个简单的方法就是  既然