ssh4 LazyInitializationException

时间:2014-02-20 09:12:02

标签: java spring hibernate struts2 lazy-evaluation

这里是stack:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session 
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164) 
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285) 
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
cn.btttech.entity.Department_$_javassist_10.getPrivileges(Department_$_javassist_10.java) 
cn.btttech.service.comm.TableColumnsService.list(TableColumnsService.java:44) 
cn.btttech.service.comm.TableColumnsService$FastClassByCGLIB$3753c7f6.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 

我尝试的解决方案:
1.web.xml

<filter> 
      <filter-name>hibernateFilter</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>      
    </filter> 

    <filter-mapping> 
      <filter-name>hibernateFilter</filter-name> 
      <url-pattern>/*</url-pattern> 
      <dispatcher>REQUEST</dispatcher> 
      <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> 

在struts2的配置前面 2.TableColumnsService类

@Service("tableColumnsService") 
@Transactional(readOnly = true) 
public class TableColumnsService extends AbstractService<TableColumns>{ 

@Transactional(readOnly = true) 
public Set<TableColumns> list(User user, Menu menu) { 

System.out.println("MenuName:"+menu.getMenuName()); 

Set<TableColumns> tableColumnsesMenu = menu.getTableColumnses(); 
Department department = user.getDepartment();
Set<Privilege> privileges = department.getPrivileges();Here is error line

3.spring交易

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 
    <!-- TransactionManager -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"  
        p:sessionFactory-ref="sessionFactory" /> 

4.user class

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "department_id") 
@Lazy(false) 
public Department getDepartment() { 
return this.department; 
} 

我很困惑,任何建议都会很感激。感谢。

1 个答案:

答案 0 :(得分:0)

认为您正在尝试访问已分离的集合...

删除@Lazy(false)并尝试使用@ManyToOne(fetch = FetchType.EAGER)

如果这样可以恢复更改并添加Hibernate.initizlize(部门);

你的映射也有错误.. 一个用户可以拥有多个部门,因此在这种情况下,用户类部门应该是一个Set而不是一个对象..

也可以使用相同的权限.. 部门对象未初始化以获得权限..