javaSE项目配置spring session的问题

hsbbhzz 2017-08-06 04:49:06
最近用到javaSE项目去集成spring、hibernate框架,已经集成好框架,写了增删改查,没有问题,但是执行hql语句就会出现问题,问题如下:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
图:

百度搜索了各种解决方案都不行,lazy=true和lazy=false都是报这个错误。请各位大神帮帮忙。
我的spring配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">


<!-- 自动扫描cn包 ,将带有注解的类 纳入spring容器管理
<context:component-scan base-package="/"></context:component-scan>
<context:component-scan base-package="ceshi"/> -->
<!-- 注册注解的后台处理类 -->
<!-- <context:annotation-config>
指明使用注解的类的包
<context:component-scan base-package="ceshi"/>
</context:annotation-config> -->
<!-- 数据库梳理器 -->
<bean id="HibernateDao" class="ceshi.dao.HibernateDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- mina服务器 -->
<bean id="MinaServer" class="ceshi.minaServer.MinaServer">
<!-- <property name="timeServerHandler">
<ref bean="TimeServerHandler" />
</property> -->
</bean>

<!--Begin Service -->
<!-- 药品模块业务层处理-->
<bean id="MedicienService" class="ceshi.service.medicien.MedicienService">
<property name="HibernateDao">
<ref bean="HibernateDao" />
</property>
</bean>


<!--End Service -->


<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url"
value="jdbc:oracle:thin:@192.168.0.22:1521:ORCL">
</property>
<!-- value="jdbc:oracle:thin:@localhost:1521:orcl"> -->
<!-- <property name="username" value="classiccars"></property>
<property name="password" value="myeclipse"></property> -->
<property name="username" value="123"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>

<value>ceshi/hibernateEntity/Person.hbm.xml</value>
</list>
</property></bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="persistence*" propagation="REQUIRED" />

<!-- hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 -->
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="count*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />

</tx:attributes>
</tx:advice>
<!-- 定义事务入口 -->
<aop:config>
<aop:pointcut id="allDaoMethod" expression="execution(* ceshi.service.*.*(..))" />
<aop:advisor advice-ref="HibernateDao" pointcut-ref="allDaoMethod" />
</aop:config>


<!-- 定义事务 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

</beans>

对于配置这块我确实不太了解,不知道是哪里的问题,还请各位大神帮忙解决下。给点思路。
...全文
231 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengpiaoxuexue 2017-08-10
  • 打赏
  • 举报
回复
你把邮箱私发我下,我这里有个例子,发给你
hsbbhzz 2017-08-07
  • 打赏
  • 举报
回复
恩,我是用hql查询,而且最后是转json数据,但是只要用hql查询就报这个错误,我的配置文件有问题吗?对自己的配置没多大信心
lifewell1 2017-08-07
  • 打赏
  • 举报
回复
你是不是hql懒加载然后转json了呀
李德胜1995 2017-08-06
  • 打赏
  • 举报
回复
org.hibernate.LazyInitializationException: could not initialize proxy - no Session。。。。 例如一的一方get得到多个一方,由于集合没有初始化,但是session已经关闭,会出现这个异常,可以在一的一方查询出来后使用 hibernate.initialize初始化集合,或者配置抓取策略为join...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧