当前位置:编程学习 > JAVA >>

SSH整合的问题,折腾死我了,求大神指教一下吧!!

一直都是用ssh三个框架做项目的,但每次整合之后跑起来,都会遇到一个问题,就是hibernate懒加载异常!
因为我的数据库表设计的关联比较多,所以在实体类中的时候配置了很多一对多和多对一关联,之所以这样做也是想利用hibernate的管理查询方便我在前台取数据,这样我的前台页面就能够充分地分离出来了。
但现在把三个框架整合起来以后,每次在页面关联显示数据时都会出现懒加载异常:
2012-12-25 13:55:09,447 ERROR (org.hibernate.LazyInitializationException:42) - failed to lazily initialize a collection of role: com.blogdiary.model.BlogHome.blogVisits, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.blogdiary.model.BlogHome.blogVisits, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:332)

其实我明白这个错误是什么意思,也知道在hibernate实体类映射文件中配lazy="false"关闭懒加载来解决,但是这样会影响效率,而且治标不治本,也不是长久之计,如果数据库表再多点用这种方法很不靠谱。
我在网上搜了可以用spring的Session过滤器解决懒加载异常的问题,但是我照着他们的方法配置就是不起作用,已经折腾很久了,一直很想解决却一直没能解决,请哪位大神帮我看看我的配置到底哪里不对啊!!我要感激涕零了都、、、、、、、、
我用的版本是Struts2,Spring3和Hibernate3.3,下面这是我的web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app-context.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 解决懒加载异常 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

我也在Spring的配置文件中配置了事务管理,但还没测试,也不知道起没起作用
    <!-- 配置事务管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 配置哪些类的哪些方法需要使用事务 -->
<aop:config>
<aop:pointcut id="allServiceMethods"
expression="execution(* com.blogdiary.service.*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethods" advice-ref="txAdvice" />
</aop:config>
真心求各位帮我解决这个问题,谢谢了!!! --------------------编程问答-------------------- 最关键的东西你没有贴出来。贴出来com.blogdiary.model.BlogHome.blogVisits这个类。 --------------------编程问答-------------------- <filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> --------------------编程问答--------------------
引用 1 楼 fangmingshijie 的回复:
最关键的东西你没有贴出来。贴出来com.blogdiary.model.BlogHome.blogVisits这个类。

这只是一个普通的实体类,没什么特别的 --------------------编程问答--------------------
引用 2 楼 JavaSven 的回复:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
……

我struts的url mapping是/*啊,这个有影响吗 --------------------编程问答-------------------- <!-- 解决懒加载异常 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

扔到spring的上面去 --------------------编程问答--------------------
引用 5 楼 ohlonger 的回复:
<!-- 解决懒加载异常 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<par……
试过了,不行 --------------------编程问答-------------------- 好久没用hibernate了。不知道具体怎么配置,大概问题就是你查询主Object后,session关闭了。
如果你的程序中没有主动关掉它。就找找配置session生命周期的地方 --------------------编程问答-------------------- 这里有一个最新的SSH框架整合例子,希望能帮到你 http://bbs.itqy8.com/topic/114 --------------------编程问答-------------------- 神呐!!!竟然没有人能搞定!! --------------------编程问答-------------------- 我对比了LZ与我写的区别,我在配置filter加了这么一段
<filter>
<filter-name>openSessionInView</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>
</filter>

<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

指定了sessionFactoryBeanName这个参数的名字,LZ试一下 --------------------编程问答--------------------
引用 10 楼 Just_szl 的回复:
我对比了LZ与我写的区别,我在配置filter加了这么一段Plain Text code?12345678910111213<filter>        <filter-name>openSessionInView</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.……

这个参数如果不指定默认就是sessionFactory的,而且我之前也加了,还是没起作用 --------------------编程问答-------------------- 注意出场顺序 啊 ,,
我写的显示hibernateSessionInViewFilter,然后再是struts2核心控制器,再是spring监听器,这主要是顺序导致的 。。 --------------------编程问答--------------------
引用 12 楼 ch656409110 的回复:
注意出场顺序 啊 ,,
我写的显示hibernateSessionInViewFilter,然后再是struts2核心控制器,再是spring监听器,这主要是顺序导致的 。。
再怎么排都没用
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,