• Now all data be ready for publish, We had made our blog alive now. Haha.



  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…



How to solve the “failed to lazily initialize a collection of role” Hibernate exception- Stack Overflow

Java PingBook 2 months ago (10-23) 21 0

Question

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 comments = new LinkedHashSet();

public Collection 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 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" %>





  • ${item.getText()}


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

Answer

If you know that you’ll want to see all Comments 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 comments = new LinkedHashSet();

Collections are lazy-loaded by default, take a look at this if you want to know more.




Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:How to solve the “failed to lazily initialize a collection of role” Hibernate exception- Stack Overflow
LIKE (0)
[1725641479@qq.com]
SHARE (0)
PingBook
Author:
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website