我有一个问题:
org.hibernate.LazyInitializationException:未能延迟初始化角色集合:mvc3.model.Topic.comments,未关闭任何会话或会话
模型如下:
@实体
@表(name=“T_主题”)
公开课主题{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@许多酮
@JoinColumn(name=“USER\u ID”)
私人用户作者;
@枚举(EnumType.STRING)
私人标签topicTag;
私有字符串名称;
私有字符串文本;
@OneToMany(mappedBy=“topic”,cascade=CascadeType.ALL)
私人收藏<;评论>;评论=新建LinkedHashSet<;评论>;();
...
公共收藏<;评论>;获取评论(){
返回评论;
}
}
调用模型的控制器如下所示:
@控制器
@请求映射(value=“/topic”)
公共类主题控制器{
@自动连线
私人话题服务;
私有静态最终记录器Logger=LoggerFactory.getLogger(TopicController.class);
@RequestMapping(value=“/details/{topicId}”,method=RequestMethod.GET)
公共模型和视图详细信息(@PathVariable(value=“topicId”)int-id)
{
Topic topicById=service.findTopicByID(id);
集合<;Comment>;commentList=topicById.getComments();
Hashtable modelData=新的Hashtable();
modelData.put(“主题”,topicById);
modelData.put(“commentList”,commentList);
返回新的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>;
<;头>;
<;标题>;查看主题</标题>;
</头>;
<;车身>;
<;ul>;
<;c:forEach items=“${commentList}”var=“item”>;
<;jsp:usebeanid=“item”type=“mvc3.model.Comment”/>;
<;李>${item.getText()}</李>;
</c:forEach>;
</ul>;
</车身>;
</html>;
查看jsp时引发异常。与c:forEach循环一致
如果您知道每次检索主题时都要查看所有注释,请将注释的字段映射更改为:
OneToMany(fetch=FetchType.EAGER,mappedBy=“topic”,cascade=CascadeType.ALL)
私人收藏<;评论>;注释=新LinkedHashSet<;评论>;();
默认情况下,集合是延迟加载的,请查看如果您想了解更多信息,请点击此处