hibernate4 could not initialize proxy - no Session

干得好差 发布于 2013/08/02 14:18
阅读 8K+
收藏 0

      用Spring3.2+hibernate4.2搭建开发框架的时候,调用load方法的时候,出现错误:org.hibernate.LazyInitializationException: could not initialize proxy - no Session

     具体配置如下:

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">
        <listener>
	    <listener-class> org.springframework.web.context.ContextLoaderListener                       </listener-class>
	</listener>
	<listener>
	   <listener-class>org.springframework.web.context.request.RequestContextListener                   </listener-class>
	</listener>
	<listener>
		<listener-class>
                     org.springframework.web.util.IntrospectorCleanupListener </listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/spring-*.xml
		</param-value>
	</context-param>                                                                          <filter>
 <filter-name>openSessionInViewFilter</filter-name>
 <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
 <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>sessionFactory</param-value>         
      </init-param>   
 <init-param>
 <param-name>singleSession</param-name>
 <param-value>true</param-value>
 </init-param>
 <init-param>
 <param-name>flushMode</param-name>
 <param-value>auto</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>openSessionInViewFilter</filter-name>
 <url-pattern>*.do</url-pattern>
 </filter-mapping>
 <filter>
 <filter-name>encodingFilter</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>
 <init-param>
 <param-name>forceEncoding</param-name>
 <param-value>true</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>                                                                               <servlet>
 <servlet-name>spring-mvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath*:/spring-*.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>/js/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>/images/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>/css/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>*.vm</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>spring-mvc</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>  <session-config>
 <session-timeout>30</session-timeout>
 </session-config>


 <welcome-file-list>
 <welcome-file>index.do</welcome-file>
 </welcome-file-list>
</web-app> 

spring-mvc-hiberante.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
		default-lazy-init="true">

<bean id="sessionFactory"                                                                                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
		<props>
		  <prop key="hibernate.dialect">${hibernate.dialect}</prop>
		 <prop key="hibernate.hbm2ddl.auto">none</prop>
		 <prop key="hibernate.show_sql">true</prop>
		 <prop key="hibernate.format_sql">true</prop>
		 <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
		 <prop key="hibernate.current_session_context_class">                                                  org.springframework.orm.hibernate4.SpringSessionContext                                    </prop>
		</props>
		</property>
	
		<property name="mappingLocations">
		   <list>
			 <value>
			  classpath:/com/test/aaa/entity/**/*.hbm.xml
			  </value>
			</list>
		</property>
	</bean>
       <bean name="transactionManager"                                                                     class= "org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
加载中
0
今夜吴眠
<may-to-one>or<one-to-may> 的 lazy 属性默认为 :lazy = "proxy"
解决 :<many-to-one>   & <set> 中设置 lazy="false"
干得好差
干得好差
我这边没有many to one的,但还是出错了。
0
不允许改性别
不允许改性别

看起来配置没有错误,我这边部分查询也遇到这个情况,很令人费解。

一般的延迟加载都可以通过配置org.springframework.orm.hibernate4.support.OpenSessionInViewFilter解决,但是我有一个通过hql查询出来的元素关联的集合配置的延迟加载却会抛出无法初始化代理的错误。。想不通啊

0
g
garrick
使用load()方法加载对象时,如果该对象的映射文件中配置了lazy="true"(实体对象默认好像就是lazy="true"),则load()方法不会立即发出语句去加载对象。当你把该对象作为一个方法的返回值,然后再调用该对象的方法时,就会报 could not initialize proxy - no Session。因为此时Hibernate才会真正的发语句到数据库,但是此时Session已经被你关闭了。总结一句话,要么用session.get()加载对象,要么配置立即加载 即lazy="false"
返回顶部
顶部