gpt4 book ai didi

java - Hibernate 带有 @Transactional 的延迟初始化异常

转载 作者:行者123 更新时间:2023-12-01 13:12:35 25 4
gpt4 key购买 nike

我在这上面浪费了太多时间,是时候在这里问一下了,谢谢。

框架:SpringMVC 4 + Hibernate 3.6.0

我的期望:在 jsp 页面上显示 Pojo(一对多)对象

我的配置

1.applicationContext.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="driverUrl" value="jdbc:mysql://localhost:3306/xxx"/>
<property name="user" value="-"/>
<property name="password" value="-"/>
<property name="maximumConnectionCount" value="10"/>
<property name="minimumConnectionCount" value="5"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="com.test.pojo"/>
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- auto generate table -->
</props>
</property>
</bean>
</beans>

2.dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">



<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<task:annotation-driven />

<mvc:annotation-driven />

<context:component-scan base-package="com.test.module" />

<!--HandlerMapping-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!--HandlerAdapter-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!--ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

3. Controller

@Transactional
@Controller
public class TESTS {

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

@RequestMapping("/hello")
public ModelAndView test(){
ModelAndView mv = new ModelAndView();
mv.setViewName("/hello");
mv.addObject("master", (Master) sessionFactory.getCurrentSession().createQuery("from Master where master_id = 1").uniqueResult());
return mv;
}
}

4.Pojos

public class Master {
@Id
@GeneratedValue
private Integer master_id;
private String master_name;

@OneToMany(fetch = FetchType.LAZY, mappedBy="master", cascade = CascadeType.ALL,orphanRemoval=true)
private List<Pet> pets;
}

public class Pet {

@Id
@GeneratedValue
private Integer id;

private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "master_id", nullable = false)
private Master master;
}

即使我把@Transnational注解放在@Controller前面,我仍然无法获取宠物的信息,总是出现异常“无法延迟初始化角色集合:com.test.pojo.Master.pets,没有 session ”或 session 已关闭'

请指教,谢谢!

最佳答案

异常说明了一切。您正在尝试从 JSP 访问延迟加载的集合。当执行 JSP 时, Controller 的事务方法已返回,并且事务及其关联的 session 将关闭。因此 Hibernate 无法再加载该集合。

确保从 Controller 加载集合,或使用 OpenSessionInViewFilter 或 OpenSessionInViewInterceptor。

从 Controller 加载集合:

  • 调用master.getPets().size()(或集合的任何其他方法),或者
  • 调用Hibernate.initialize(master.getPets()),或
  • 在单个查询中加载主人及其宠物:select m from Master m left join fetch m.pets where...

请注意,当您当前的代码应该简单地使用 session.get() 通过 ID 获取 Master 时,它会使用查询。

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

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