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.
Sorry, but i'd like to use lazy-load. So, I've changed the 'LinkedHashSet' type t the 'PersistentList'. Exception still occurs
This could be used as a workaround, but not an actual solution to the problem. What if we need to fetch lazily?
but in case if we want lazy then this solution will not work and most of the cases we want lazy only.
This is the type of answer that pops up everywhere on stack overflow. Short, to the point, solves the problem and MISLEADING. To future readers, do yourself a favor and learn what exactly is lazy and eagerly fetched, and understand the consequences.
@darrengorman When I started JPA I posted a question around the lines of OP's one. I received the same response as you gave. Soon enough when I ran some test with hundreds of thousands of row, guess what happened ? I think it is misleading because it provides too simple of an answer for a problem that mostly beginners will face and soon enough they will have their whole database loaded in memory if they are not careful (and they won't, because they won't be aware of it) :).