Hibernate OpenSessionInViewFilter问题

问题描述

使用springmvc、spring3.0.5、hibernate3整的一个框架,hibernate使用懒加载模式时会报couldnotinitializeproxy-noSession,于是配置了OpenSessionInViewFilter,可是还是报错,通过关联spring的源码后debug,这个过滤器确实是执行了,并且创建了Session,然后去Dao层通过SessionFactory.getCurrentSession()执行操作,如下代码:protectedSessiongetSession(){Sessionsession=sessionFactory.getCurrentSession();Sessionsession1=SessionFactoryUtils.doGetSession(sessionFactory,true);LOGGER.info("daoSession:"+session);LOGGER.info("filterSession:"+session1);LOGGER.info(session==session1);returnsession;}代码中session是通过依赖注入的sessionFactory获取的当前session,session1这种方式是查看spring的OpenSessionInViewFilter源码中拷过来的,日志输出session==session1为true,其中的一个dao操作为:@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)publicTfind(SerializableentityId){//return(T)getSession().get(clazz,entityId);return(T)getSession().load(clazz,entityId);}但是执行这个find方法结果还是报couldnotinitializeproxy-noSession,这就奇怪了,难道session在执行完find方法后就被关闭了?所以在返回到jsp中使用el表达式取值的时候session已经关闭了?求大神指点迷津!!

解决方案

解决方案二:

http://www.yybean.com/opensessioninviewfilter-role-and-configuration
解决方案三:

那篇文章看过了,但是对于解决我的问题效果不大啊。。。
解决方案四:

我得看看你的OpenSessionInViewFilter怎么配置的
解决方案五:

之前我项目中出现过这个问题后来检查发现是我配置的SessionFactory弄错了比如你这个查询是用的aSessionFactory但是你open里面配置的是bSessionFactory这样a还是会报错
解决方案六:

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!--<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">--><propertyname="dataSource"ref="dataSource"/><propertyname="packagesToScan"><list><value>com.jiangnan.cms.domain</value></list></property><propertyname="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialecthibernate.show_sql=truehibernate.format_sql=falsehibernate.query.substitutions=true1,false0hibernate.jdbc.batch_size=20hibernate.hbm2ddl.auto=updatehibernate.cache.use_query_cache=false</value></property></bean><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><tx:annotation-driventransaction-manager="transactionManager"/>以上是sessionFactory及事务的配置
解决方案七:

跟OpenSessionInViewFilter配置的顺序有关
解决方案八:

<filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/cms/*</url-pattern></filter-mapping><filter><filter-name>osivFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>osivFilter</filter-name><url-pattern>/cms/*</url-pattern></filter-mapping><!--权限配置--><filter><filter-name>privilegeFilter</filter-name><filter-class>com.jiangnan.cms.filter.PrivilegeFilter</filter-class></filter><filter-mapping><filter-name>privilegeFilter</filter-name><url-pattern>/cms/admin/*</url-pattern></filter-mapping><filter><filter-name>contextFilter</filter-name><filter-class>com.jiangnan.cms.filter.ContextFilter</filter-class></filter><filter-mapping><filter-name>contextFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>FileUploadFilter</filter-name><filter-class>com.ckfinder.connector.FileUploadFilter</filter-class><init-param><param-name>sessionCookieName</param-name><param-value>JSESSIONID</param-value></init-param><init-param><param-name>sessionParameterName</param-name><param-value>jsessionid</param-value></init-param></filter><filter-mapping><filter-name>FileUploadFilter</filter-name><url-pattern>/ckfinder/core/connector/java/connector.java</url-pattern></filter-mapping>以上五个filter是web.xml中所有的filter,第一个是设置编码的,第二个就是openSessionInViewFilter,第三个是自定义的,做权限控制的,第四个是也是自定义的,主要将请求request放入ThreadLocal中,以便其他地方使用request,第五个是ckfinder使用到的。这五个filter的绝对顺序是上面这样的,其中前面四个顺序是紧挨着的,第四个和第五个filter之间还有一些servlet的配置,filter的顺序或者拦截的url有啥问题吗?

时间: 2024-07-30 20:50:22

Hibernate OpenSessionInViewFilter问题的相关文章

hibernate3-spring mvc OpenSessionInViewFilter hibernate 懒加载问题

问题描述 spring mvc OpenSessionInViewFilter hibernate 懒加载问题 之前做了一个项目是使用 springmvc 加hibernate 做的, 并且使用了 hibernate 的懒加载有用到OpenSessionInViewFilter, 现在做另一个项目, 基础代码都是上个项目的代码: 目前调试在调试权限模块, 核对了上个项目的代码基本完全一致,但是这个项目的老是无法使用懒加载,加载报 org.hibernate.LazyInitializationE

关于Spring的OpenSessionInViewFilter的Hibernate会话

问题描述 <class name="com.cmmi.bcm.data.BcmGood" table="bcm_good" schema="dbo" lazy="true">这个表有N个外表关联,然后再web.xml中加上了过滤,成功的在web展示页面中显示每个BcmGood的数据,但是我加了WorkHibernateFilter过滤以后,其他(单表业务)模块的根本不能做相应业务处理(saveorupdate,de

spring+hibernate避免延迟加载异常

在hibernate中,延迟加载是1个非常大的优点,但有时候却给我们带来一些小麻烦,在后台查询结束后,session已经关闭,但在前台显示的时候,如果存在关联关系就会产生延迟加载异常. 解决办法是客户端每次请求就分配1个session,将请求结果返回给客户端,并完成展现后关闭session. 实现这个功能非常简单,在web.xml中加入以下配置 <filter> <filter-name>hibernateFilter</filter-name> <filter-

Hibernate和Spring的延迟加载和DAO模式

Hibernate和延迟加载 Hibernate对象关系映射提供了两种对象初始化模式:延迟加载和非延迟加载.非延迟加载在加载时获取对象本身以及它关联的所有对象.这可能导致在获取一个实例时,执行成百上千的select语句.当使用双向关联时,这个问题被放大,常常出现初始化请求时,整个数据库都被载入.显然检查每个对象的关系,并手工删除他们会费点事,但最终我们可能会因此丢失使用ORM工具的优势.一个明细的解决方式是使用hibernate提供的延迟载入机制.这种初始化策略在类成员被访问时只载入它的一个对象

hibernate使用memcached作为二级缓存,怎么构建memcached集群

问题描述 hibernate使用memcached作为二级缓存,怎么构建memcached集群 我使用memcached作为hibernate的二级缓存实现,在memcached单节点的情况下,程序运行没问题.后来想做一个memcached的集群,就使用了magent来做.但是使用magent之后,hibernate的二级缓存就报超时异常:16:29:13832 DEBUG ReadWriteCache:160 - Cached: com.ct.po.TCtBizstatusType#10011

web app-项目找不到hibernate.cfg.xml也找不到applicationContext.xml

问题描述 项目找不到hibernate.cfg.xml也找不到applicationContext.xml 公司项目框架是ssh2使用注解开发.这两个文件是不是必须的?为什么会找不到呢web.xml配置如下<?xml version=""1.0"" encoding=""UTF-8""?> innermanager contextConfigLocation classpath:/config/spring/*.x

springmvc-请教:Spring + Hibernate 无法将数据写入数据库?

问题描述 请教:Spring + Hibernate 无法将数据写入数据库? Spring + Hibernate 无法将数据写入数据 请教: 通过Junit单元测试Service可以将数据写入数据库:但部署访问却无法向数据库写入数据. 1 环境: Spring 3.1.2 Hibernate 4.1.4 Jdk1.6 2 配置: 2.1 Web.xml <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring C

spring 的OpenSessionInViewFilter简介

假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor.session会在transaction结束后关闭. 如果你采用了spring的声明式事务模式,它会对你的被代理对象的每一个方法进行事务包装(AOP的方式).如下:  <bean id="txProxyTemplate" abstract="true"

Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释

@jsonignore的作用作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. http://www.cnblogs.com/toSeeMyDream/p/4437858.html 当表间有One2Many或Many2One时,会发生无限循环的场景,如何破? 只要在Set方法前增加以下注解即可: @JsonIgnore public Set xxxs() { return this.xxxYyyy; } http://www.cnblogs.com/tomp