gpt4 book ai didi

java - ssh4 延迟初始化异常

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:41 26 4
gpt4 key购买 nike

这是堆栈:

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.用户类别

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

我很困惑,任何建议将不胜感激。谢谢。

最佳答案

认为您正在尝试访问一个独立的集合...

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

如果这有效,则恢复更改并添加 Hibernate.initizlize(department);

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

也尝试同样的权限..部门对象未初始化以获取权限..

关于java - ssh4 延迟初始化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21903098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com