could not initialize proxy - no Session 2022-08-01 13:40 146阅读 0赞 #### 这是一个精典的问题: #### #### 因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy对象。只有当们具体用到哪个get\*\*方法时才会发sql语句,才会去数据库查。但是当我们把打开session,关闭session交给了srping去做时,当们load完之后我们的session就会被srping关闭,如果我们在jsp页面或者其它的地方再去用get方法取值时就会报这个错误。 #### #### 解决方法一:但如果我们在hibernate用get方法就可以解决取单个对象的问题,因为get方法直接发sql语句,把我们想的数据从数据库中get出来然后放在内存中。 #### #### 如果我们取单个对象可以用get方法没有问题;但是如果我们取的的对象还有关联对象时用get就有问题,因为它不会把关联的对象取出来,但如果页面上用到关联的对象时也会报nosession的问题 #### #### 解决方法二:用到srping的filter(要加在strutsfilter有前面,因为它也有先后顺序,有先进先出的原则) #### #### 在我们的web.xml里面加上 #### #### #### <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> #### #### #### 这样做就是让opensessionclosesession全交给视图部分,最后视图部分用完了session再去关session就不会有上面的错误了 #### 情况二:但是使用上面的方式又会出现新的问题Write operations are not allowed in read-only mode写操作不被允许 错误原因: OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。 ## 解决办法: ## web.xml配置里添加 <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name> flushMode </param-name> <param-value>AUTO </param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 如果在交给spring 管理的情况下,在beans.xml 里的配置 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(* com.fan.service.base.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="save*" propagation="REQUIRED"/> // 如果不把save update delete都配置上, <tx:method name="update*" propagation="REQUIRED"/> //这些操作会无效 <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
相关 could not initialize proxy - no Session 这是一个精典的问题: 因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy 墨蓝/ 2021年06月24日 14:00/ 0 赞/ 368 阅读
相关 could not initialize proxy - no Session 这是一个精典的问题: 因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy 亦凉/ 2021年09月25日 21:52/ 0 赞/ 263 阅读
相关 Spring Data JPA 报错 could not initialize proxy – no Session 解决方案 Could not initialize proxy - no Session 在启动项目后报错 [![Spring Data JPA 报错 could not ゝ一纸荒年。/ 2022年02月15日 04:57/ 0 赞/ 180 阅读
相关 hibernate懒加载异常:could not initialize proxy - no Session解决办法 hibernate懒加载异常,通常是在查询一个对象,然后需要获取该对象的关联对象属性时出现的。对于这种错误解决办法有三种: 1、关闭懒加载,lazy=false。这种方式会在 系统管理员/ 2022年03月02日 11:55/ 0 赞/ 471 阅读
相关 could not initialize proxy - no Session could not initialize proxy - no Session 你是不是已经配置了`openSessionInView`还报这个错,不是,那还不去配置!! - 日理万妓/ 2022年06月17日 05:53/ 0 赞/ 46 阅读
相关 异常Hibernate:could not initialize proxy - no Session 这个从字面上就可以看出:不能初始化,没有session。也就说主要原因是因为session关闭了。 在[hibernate][]中,<many-to-one…/>中的l 谁借莪1个温暖的怀抱¢/ 2022年07月11日 08:18/ 0 赞/ 181 阅读
相关 JPA中的could not initialize proxy - no Session异常分析与解决 引言: JPA是一种非常流行和常用的持久化框架标准,其下可以对接若干种不同的实现,在不同的父子表管理中,经常会碰到no Session的问题,该如何解决呢? 1. 问题的引出 r囧r小猫/ 2022年07月15日 13:40/ 0 赞/ 90 阅读
相关 JPA之"could not initialize proxy - no Session"的异常解决 引言: 在使用JPA中经常会碰到各类问题,这里列出解决no Session问题的方法。 1. 异常信息的描述 这里列出异常栈, 关键词是无法初始化proxy,在读取 女爷i/ 2022年07月18日 11:58/ 0 赞/ 601 阅读
相关 could not initialize proxy - no Session 这是一个精典的问题: 因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个pro 我会带着你远行/ 2022年08月01日 13:40/ 0 赞/ 147 阅读
相关 org.hibernate.LazyInitializationException: could not initialize proxy [beans.Student#1] - no Session 转载出处:https://www.bilibili.com/video/BV1uT4y1g7Qo?p=431&spm\_id\_from=pageDriver 分析:Acti 素颜马尾好姑娘i/ 2023年01月13日 15:55/ 0 赞/ 43 阅读
还没有评论,来说两句吧...