我有这样一个问题:

org.hibernate.LazyInitializationException:惰性初始化role: 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;
    }

}

调用model的控制器如下所示:

@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时,将引发异常。在c:forEach循环的行中


当前回答

为了摆脱惰性初始化异常,在操作分离对象时不应该调用惰性收集。

在我看来,最好的方法是使用DTO,而不是实体。在这种情况下,您可以显式地设置您想要使用的字段。像往常一样,这就足够了。无需担心由Lombok生成的jackson ObjectMapper或hashCode之类的东西会隐式调用您的方法。

对于某些特定的情况,您可以使用@EntityGrpaph注释,它允许您即使在实体中有fetchType=lazy也可以进行即时加载。

其他回答

如果你知道你想在每次检索主题时看到所有的注释,那么将注释的字段映射更改为:

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

默认情况下,集合是惰性加载的,如果你想了解更多,可以看看这个。

为了延迟加载集合,必须有一个活动会话。在web应用中,有两种方法可以做到这一点。您可以使用Open Session In View模式,其中使用拦截器在请求开始时打开会话,并在结束时关闭会话。风险在于你必须有可靠的异常处理,否则你可能会绑定所有的会话,你的应用程序可能会挂起。

The other way to handle this is to collect all the data you need in your controller, close your session, and then stuff the data into your model. I personally prefer this approach, as it seems a little closer to the spirit of the MVC pattern. Also if you get an error from the database this way you can handle it a lot better than if it happens in your view renderer. Your friend in this scenario is Hibernate.initialize(myTopic.getComments()). You will also have to reattach the object to the session, since you're creating a new transaction with every request. Use session.lock(myTopic,LockMode.NONE) for that.

还有另一种方法,可以使用TransactionTemplate封装惰性获取。 就像

Collection<Comment> commentList = this.transactionTemplate.execute
(status -> topicById.getComments());

通过使用hibernate @Transactional注释,如果你从数据库中获得一个具有惰性获取属性的对象,你可以像这样简单地获取这些属性:

@Transactional
public void checkTicketSalePresence(UUID ticketUuid, UUID saleUuid) {
        Optional<Ticket> savedTicketOpt = ticketRepository.findById(ticketUuid);
        savedTicketOpt.ifPresent(ticket -> {
            Optional<Sale> saleOpt = ticket.getSales().stream().filter(sale -> sale.getUuid() == saleUuid).findFirst();
            assertThat(saleOpt).isPresent();
        });
}

这里,在Hibernate代理管理的事务中,调用ticket.getSales()执行另一个查询来获取sales,因为您显式地请求了它。

处理LazyInitializationException的最好方法是在查询时加入取回,像这样:

select t
from Topic t
left join fetch t.comments

您应该始终避免以下反模式:

FetchType。急切的 OSIV (Open Session in View) 冬眠。enable_lazy_load_no_trans Hibernate配置属性

因此,确保你的FetchType。LAZY关联在查询时或使用Hibernate在原始的@Transactional范围内初始化。初始化辅助集合。