hibernate3-spring mvc OpenSessionInViewFilter hibernate 懒加载问题

问题描述

spring mvc OpenSessionInViewFilter hibernate 懒加载问题

之前做了一个项目是使用 springmvc 加hibernate 做的, 并且使用了 hibernate 的懒加载有用到OpenSessionInViewFilter, 现在做另一个项目, 基础代码都是上个项目的代码;

目前调试在调试权限模块, 核对了上个项目的代码基本完全一致,但是这个项目的老是无法使用懒加载,加载报 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.user.role.Role.menuRole, no session or session was closed, 和上个项目真的完全一致了, 包括事务都看过没问题,
下面是两个项目的log4j 日志, 是不一样的

-- 上个项目的日志
[DEBUG] [http-8088-2] org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
[DEBUG] [http-8088-2] org.hibernate.loader.Loader - result row: EntityKey[com.zmis.model.user.privilege.Role#297e26b04859021c01485902252d0004]
[DEBUG] [http-8088-2] org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
[DEBUG] [http-8088-2] org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[DEBUG] [http-8088-2] org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.zmis.model.user.privilege.Role#297e26b04859021c01485902252d0004]
[DEBUG] [http-8088-2] org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.zmis.model.user.privilege.Role#297e26b04859021c01485902252d0004]
[DEBUG] [http-8088-2] org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
[DEBUG] [http-8088-2] org.hibernate.loader.Loader - done entity load
[DEBUG] [http-8088-2] org.springframework.orm.hibernate3.HibernateTemplate - Not closing pre-bound Hibernate Session after HibernateTemplate

--- 现在这个项目的日志
[DEBUG] [http-8088-1] org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
[DEBUG] [http-8088-1] org.hibernate.loader.Loader - result row: EntityKey[com.zmis.model.user.role.Role#297e26b049520e6d0149520e746a000b]
[DEBUG] [http-8088-1] org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
[DEBUG] [http-8088-1] org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[DEBUG] [http-8088-1] org.hibernate.engine.TwoPhaseLoad - resolving associations for [com.zmis.model.user.role.Role#297e26b049520e6d0149520e746a000b]
[DEBUG] [http-8088-1] org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.zmis.model.user.role.Role#297e26b049520e6d0149520e746a000b]
[DEBUG] [http-8088-1] org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
[DEBUG] [http-8088-1] org.hibernate.loader.Loader - done entity load
[DEBUG] [http-8088-1] org.springframework.orm.hibernate3.HibernateTemplate - Eagerly flushing Hibernate session
[DEBUG] [http-8088-1] org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
[DEBUG] [http-8088-1] org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
[DEBUG] [http-8088-1] org.hibernate.engine.Collections - Collection found: [com.zmis.model.user.role.Role.menuRole#297e26b049520e6d0149520e746a000b], was: com.zmis.model.user.role.Role.menuRole#297e26b049520e6d0149520e746a000b
[DEBUG] [http-8088-1] org.hibernate.engine.Collections - Collection found: [com.zmis.model.user.role.Role.usersRole#297e26b049520e6d0149520e746a000b], was: com.zmis.model.user.role.Role.usersRole#297e26b049520e6d0149520e746a000b
[DEBUG] [http-8088-1] org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
[DEBUG] [http-8088-1] org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 2 collections
[DEBUG] [http-8088-1] org.hibernate.pretty.Printer - listing entities:
[DEBUG] [http-8088-1] org.hibernate.pretty.Printer - com.zmis.model.user.role.Role{fid=297e26b049520e6d0149520e746a000b, inputDate=2014-10-27 22:41:59, roleKey=manager, px=null, usersRole=, remark=null, menuRole=, delete=false, active=true, roleName=管理员}
[DEBUG] [http-8088-1] org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
[DEBUG] [http-8088-1] org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[DEBUG] [http-8088-1] org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!

解决方案

有大神帮忙看看, 坐等回复;

解决方案二:

遇到了同样的问题,
2015 四月 29 14:19:28[huanbaoms] WARN [http-apr-8081-exec-10] org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler.logWarning(232) |SQL Warning Code: 0, SQLState: null
2015 四月 29 14:19:28[huanbaoms] WARN [http-apr-8081-exec-10] org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler.logWarning(233) |Connection readOnly mode is not enforcable after the connection has been established. To enforce a read only connection, set the readOnly data source or connection property.
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.yt.authorization.model.TPubSysModuleinfo.subAuths, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)

at com.yt.authorization.controller.AuthorControll.parseArr(AuthorControll.java:76)
at com.yt.authorization.controller.AuthorControll.parseArr(AuthorControll.java:78)
at com.yt.authorization.controller.AuthorControll.findAuthor(AuthorControll.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)

at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.yt.filter.LoginFilter.doFilter(LoginFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
时间: 2024-10-01 04:17:58

hibernate3-spring mvc OpenSessionInViewFilter hibernate 懒加载问题的相关文章

Hibernate懒加载解析

Hibernate懒加载解析 在Hibernate框架中,当我们要访问的数据量过大时,明显用缓存不太合适, 因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,这时Hibernate用懒加载机制来弥补这种缺陷,但是这只是弥补而不是用了懒加载总体性能就提高了. 我们所说的懒加载也被称为延迟加载,它在查询的时候不会立刻访问数据库,而是返回代理对象,当真正去使用对象的时候才会访问数据库.    实现懒加载的前提:  1 实体类不能是final的 2 能实现懒加载的对象都是被CGLIB(反射调用)改

hibernate懒加载能用get方法吗

问题描述 hibernate懒加载能用get方法吗 hibernate中的session.load()方法特性是使用懒加载,那么请问hibernate中的session.get()方法在什么情况下也是使用的懒加载?还是说get方法没有懒加载,都是立即加载?看教程的时候发现测试集合的懒加载是用get方法取一对多的一对象而不是load方法 解决方案 http://blog.csdn.net/yaorongwang0521/article/details/7074573 解决方案二: get和load

hibernate懒加载问题 求救!

问题描述 <hibernate-mapping><classname="model.Team"table="team"lazy="true"><idname="id"><!--id的产生方式是uuid.hex--><generatorclass="uuid.hex"/></id><propertyname="teamNa

【spring bean】spring中bean的懒加载和depends-on属性设置

项目结构如下:   ResourceBean.java代码: 1 package com.it.res; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class ResourceBean { 9 10 private FileOutputStream out;

springmvc的jackson 和hibernate懒加载问题

问题描述 在springmvc转对象为json时,如果对象有懒加载,会报错.我所知解决办法有两种,但是都不能解决我的问题1.在实体类声明处加上@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})注解(无任何效果)2.使用jackson-module-hibernate,通过objectmapper注册个module解决,但是set集合会报错,单个懒加载对象没问题

spring 单例多例,默认,懒加载,初始化调用

(1)spring 默认是单例, 单例情况下:所有线程对于同一个类共同拥有一个对象,此时如果在类里面创建一个类变量如下 由于所有线程共享一个类对象,所以也共享一个类变量,每次请求都会增加 @Controller @RequestMapping("/user") public class User { private int i=0;//类变量 @RequestMapping("/list.shtm") public String list() { i++; Syst

lazy-hibernate 懒加载机制及抓取策略

问题描述 hibernate 懒加载机制及抓取策略 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.gycn.model.Hotel.roomTypes, no session or session was closed 出现这种情况是因为用了懒加载,但是有的地方需要加载集合,有的地方则不用,那么lazy这个属性到底要怎么设置呢? 解决方案 抓取策略应

spring-关于懒加载异常,请问怎么解决?多谢各位 了

问题描述 关于懒加载异常,请问怎么解决?多谢各位 了 我的数据库语句是from Survey s where s.user.id = ? 出现User 懒加载,我想问除了在User的配置文件中关闭懒加载这个解决方法外 1,还能用什么方法 希望能就此题具体说说 2,为什么这样用不行啊 from Survey s LEFT OUTER JOIN FETCH s.user.id = ? 3,from Survey s where s.user.id = 这句话怎么用嵌套查询 (就是分开s.user.i

hibernate+spring mvc, 解决hibernate 对象懒加载 json序列化问题

引用地址 在使用Spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例:   Java代码   @RequestMapping("login")       @ResponseBody       public Object login(@RequestParam String username,@RequestParam String password){           List<User> list=user