使用@ManyToOne并延时加载出现的问题以及解决总结
使用HIBERNATE的注解@ManyToOne(fetch = FetchType.lazy) 时候,报出错误
org.hibernate.LazyInitializationException: could not initialize proxy - no Sessionat org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)at com.evolution.warlock.entity.Department_$$_javassist_1.getName(Department_$$_javassist_1.java)at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
fetch=FetchType.LAZY就等于打开了延迟加载,Hibernate中的get方法不论是否开启延迟加载都会一次加载所有基本数据类型属性的值,而load则不同,开启延迟加载后load(id),返回的对象会被Hibernate加一个壳(持久化上下文中没有缓存该对象的前提下),拦截所有非id属性的访问操作(getter,setter调用),只有id属性中有实际值(其实就是你调用load方法时传的那个),在session没有关闭的之前,如果访问除id外的其他属性才会发sql语句去查询,你这个错误就是因为在当前session关闭以后访问由load()加载的对象的非id属性,此时Hibernate尝试通过当前session发sql查询,但发现session已经关闭,这样才会报错。
<filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern></filter-mapping>