最近用到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>
对于配置这块我确实不太了解,不知道是哪里的问题,还请各位大神帮忙解决下。给点思路。