为什么在查询期间调用JPA @PreUpdate注释的方法? [英] Why does a JPA @PreUpdate-annotated method get called during a query?

查看:320
本文介绍了为什么在查询期间调用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.

推荐答案

规范说:

PreUpdate和PostUpdate回调发生在 数据库分别对实体数据进行更新操作.这些数据库 实体状态更新时或它们发生时可能会发生操作 可能会在状态刷新到数据库时发生(可能是 在交易结束时).

The PreUpdate and PostUpdate callbacks occur before and after the database update operations to entity data respectively. These database operations may occur at the time the entity state is updated or they may occur at the time state is flushed to the database (which may be at the end of the transaction).

在这种情况下,调用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注释的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆