解决“未能懒惰地初始化角色集合”例外 [英] Solve "failed to lazily initialize a collection of role" exception
问题描述
我有这个问题:
org.hibernate.LazyInitializationException:无法延迟初始化角色集合:mvc3.model.Topic
以下是模型:
< pre $
@Entity
@Table(name =T_TOPIC)
public class Topic {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name =USER_ID)
私人用户作者;
@Enumerated(EnumType.STRING)
私有标签topicTag;
私人字符串名称;
私人字符串文本;
@OneToMany(mappedBy =topic,cascade = CascadeType.ALL)
private Collection< Comment> comments = new LinkedHashSet< Comment>();
...
公开收藏<评论> getComments(){
返回评论;
$ b
调用模型外观的控制器如下所示:
@Controller
@RequestMapping(value =/ topic)
public class TopicController {
@Autowired
私人TopicService服务;
private static final Logger logger = LoggerFactory.getLogger(TopicController.class);
@RequestMapping(value =/ details / {topicId},method = RequestMethod.GET)
public ModelAndView详细信息(@PathVariable(value =topicId)int id)
{
主题topicById = service.findTopicByID(id);
收藏<评论> commentList = topicById.getComments();
Hashtable modelData = new Hashtable();
modelData.put(topic,topicById);
modelData.put(commentList,commentList);
返回新的ModelAndView(/ topic / details,modelData);
}
}
jsp-
<%@ page import =com.epam.mvc3.helpers.Utils%> ;
<%@ page language =javacontentType =text / html; charset = UTF-8pageEncoding =UTF-8%>
<%@ taglib uri =http://java.sun.com/jsp/jstl/coreprefix =c%>
<%@ page session =false%>
< html>
< head>
< title>查看主题< / title>
< / head>
< body>
< ul>
< c:forEach items =$ {commentList}var =item>
< jsp:useBean id =itemtype =mvc3.model.Comment/>
< li> $ {item.getText()}< / li>
< / c:forEach>
< / ul>
< / body>
< / html>
查看jsp时异常升级。符合 c:forEach 循环
如果您知道您希望看到所有评论
s每当您检索主题
时,请更改评论的字段映射 code>到:
@OneToMany(fetch = FetchType.EAGER,mappedBy =topic,cascade = CascadeType。全部)
私人收藏<评论> comments = new LinkedHashSet< Comment>();
默认情况下,集合是延迟加载的,请查看这,如果你想知道更多。
I have this problem:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mvc3.model.Topic.comments, no session or session was closed
Here is the model:
@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);
}
}
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>
Exception is rised, when viewing jsp. In the line with c:forEach loop
If you know that you'll want to see all Comment
s every time you retrieve a Topic
then change your field mapping for comments
to:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "topic", cascade = CascadeType.ALL)
private Collection<Comment> comments = new LinkedHashSet<Comment>();
Collections are lazy-loaded by default, take a look at this if you want to know more.
这篇关于解决“未能懒惰地初始化角色集合”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!