问题

我有这个问题:

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;
    }

}

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

@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页面看起来如下:

<%@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时会出现例外情况。在withc:forEachloop中


#1 热门回答(148 赞)

如果你知道每次检索aTopic时都要查看全部Comment,那么请将字段映射更改为comments

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

默认情况下,集合是延迟加载的,如果你想了解更多信息,请查看this


#2 热门回答(127 赞)

根据我的经验,我有以下方法来解决着名的LazyInitializationException:
(1)使用Hibernate.initialize

Hibernate.initialize(topics.getComments());

(2)使用JOIN FETCH
你可以在JPQL中使用JOIN FETCH语法显式获取子集合。这有点像EAGER的提取方式。
(3)使用OpenSessionInViewFilter
LazyInitializationException经常出现在视图层中。如果使用Spring框架,则可以使用OpenSessionInViewFilter。但是,我不建议你这样做。如果不正确使用,可能会导致性能问题。


#3 热门回答(37 赞)

##问题的根源:

默认情况下,hibernate懒惰地加载集合(关系),这意味着当你在代码中使用collection时(此处为commentsfield inTopicclass),hibernate从数据库中获取,现在的问题是你在控制器中获取集合(JPA会话关闭的地方) 。这是导致异常的代码行(在这里加载了3882386589collection):

Collection<Comment> commentList = topicById.getComments();

你正在控制器中获取"comments"集合(topic.getComments())(whereJPA session已结束)并导致异常。此外,如果你在你的jsp文件中有这样的2666118949收集(而不是在你的控制器中得到它):

<c:forEach items="topic.comments" var="item">
//some code
</c:forEach>

出于同样的原因,你仍会有相同的例外。

##解决问题:

因为你只能在Entity类中使用FetchType.Eager(急切获取的集合)只有两个集合,并且因为延迟加载比急切加载更有效,我认为这种解决问题的方法比仅仅将FetchType更改为渴望更好:

如果你想初始化集合延迟,并使这项工作,最好将这段代码添加到yourweb.xml

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这段代码的作用是它将增加你的JPA session的长度,因为文档说,它使用了"to allow for lazy loading in web views despite the original transactions already being completed."这样JPA会话将打开更长一点,因此你可以在jsp文件和控制器类中懒洋洋地加载集合。


原文链接