无法延迟初始化角色集合

无法延迟初始化角色集合

本文介绍了如何解决Hibernate异常“无法延迟初始化角色集合"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题:

org.hibernate.LazyInitializationException:未能延迟初始化角色集合:mvc3.model.Topic.comments,没有会话或会话被关闭

这是模型:

@Entity
@Table(name = "T_TOPIC")
public class Topic {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    @JoinColumn(name="USER_ID")
    private User author;

    @Enumerated(EnumType.STRING)
    private Tag topicTag;

    private String name;
    private String text;

    @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
    private Collection<Comment> comments = new LinkedHashSet<Comment>();

    ...

    public Collection<Comment> getComments() {
           return comments;
    }

}

调用模型的控制器如下所示:

The controller, which calls model looks like the following:

@Controller
@RequestMapping(value = "/topic")
public class TopicController {

    @Autowired
    private TopicService service;

    private static final Logger logger = LoggerFactory.getLogger(TopicController.class);


    @RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
    public ModelAndView details(@PathVariable(value="topicId") int id)
    {

            Topic topicById = service.findTopicByID(id);
            Collection<Comment> commentList = topicById.getComments();

            Hashtable modelData = new Hashtable();
            modelData.put("topic", topicById);
            modelData.put("commentList", commentList);

            return new ModelAndView("/topic/details", modelData);

     }

}

jsp 页面如下所示:

The jsp-page looks li the following:

<%@page import="com.epam.mvc3.helpers.Utils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
      <title>View Topic</title>
</head>
<body>

<ul>
<c:forEach items="${commentList}" var="item">
<jsp:useBean id="item" type="mvc3.model.Comment"/>
<li>${item.getText()}</li>

</c:forEach>
</ul>
</body>
</html>

查看jsp时出现异常.与 c:forEach 循环

Exception is rised, when viewing jsp. In the line with c:forEach loop

推荐答案

根据我的经验,我有以下方法来解决著名的 LazyInitializationException:

From my experience, I have the following methods to solved the famous LazyInitializationException:

(1) 使用 Hibernate.initialize

Hibernate.initialize(topics.getComments());

(2) 使用 JOIN FETCH

您可以在 JPQL 中使用 JOIN FETCH 语法来显式提取子集合.这有点像 EAGER fetching.

You can use the JOIN FETCH syntax in your JPQL to explicitly fetch the child collection out. This is some how like EAGER fetching.

(3) 使用 OpenSessionInViewFilter

LazyInitializationException 经常发生在视图层.如果使用 Spring 框架,则可以使用 OpenSessionInViewFilter.但是,我不建议您这样做.如果使用不当,可能会导致性能问题.

LazyInitializationException often occur in view layer. If you use Spring framework, you can use OpenSessionInViewFilter. However, I do not suggest you to do so. It may leads to performance issue if not use correctly.

这篇关于如何解决Hibernate异常“无法延迟初始化角色集合"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:56