为什么在查询期间调用JPA

为什么在查询期间调用JPA

本文介绍了为什么在查询期间调用JPA @PreUpdate注释的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命名查询,该查询返回实体的Collection.

I have a named query that returns a Collection of entities.

这些实体在其上具有@PreUpdate注释的方法.在query.getResultList()中调用此方法.因此,实体在持久性上下文中发生了变化,这意味着在提交事务后,该实体将写回到数据库中.

These entities have a @PreUpdate-annotated method on them. This method is invoked during query.getResultList(). Because of this, the entity is changed within the persistence context, which means that upon transaction commit, the entity is written back to the database.

这是为什么? JPA 2.0规范没有明确提到@PreUpdate应该由查询执行调用.

Why is this? The JPA 2.0 specification does not mention explicitly that @PreUpdate should be called by query execution.

推荐答案

规范说:

在这种情况下,调用query.getResultList()会触发em.flush(),以便查询可以包括当前EntityManager会话中的更改. em.flush()将所有更改推送到数据库(进行所有UPDATE,INSERT调用).在通过JDBC @PreUpdate发送UPDATE之前,将调用相应的钩子.

In this case calling query.getResultList() triggers a em.flush() so that the query can include changed from current EntityManager session. em.flush() pushes all the changes to the database (makes all UPDATE,INSERT calls). Before UPDATE is sent via JDBC @PreUpdate corresponding hooks are called.

这篇关于为什么在查询期间调用JPA @PreUpdate注释的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:43