为什么我看不到使用Spring Data JPA通过更新查询发出的更改? [英] Why do I not see changes issued through an update query with Spring Data JPA?

查看:127
本文介绍了为什么我看不到使用Spring Data JPA通过更新查询发出的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下服务:

@Service
public class CamelService {

    @Transactional
    public aCamelThing() {

          Camel camel = this.camelRepository.findOne(1);

          System.out.println(camel.getCamelName());  // prints "normalCamel"

          // simple hql set field 'camelName'
          int res = this.camelRepository.updateCamelName(1,"superCamel!");

          Camel camelWithNewName = this.camelRepository.findOne(1);

          System.out.println(camelWithNewName .getCamelName());  // prints "normalCamel" ?!?!?

    } 
}

任何想法我怎样才能实现第二个println将打印的目标:superCamel! ?
(将第二次调用与新交易分开并不理想)。

Any idea how can i achieve the goal that the second println will print: "superCamel!" ? (separating the second call to a new transaction is not ideal).

推荐答案

你认为这个工作的原因是它的工作非常简单:JPA被定义为以这种方式工作。

The reason you see this working as it works is quite simple: JPA is defined to work that way.

我假设您触发 updateCamelName(...)的更新查询。 JPA规范声明了以下更新和删除操作:

I am assuming you trigger an update query for updateCamelName(…). The JPA specification states the following for update and delete operations:


持久性上下文与批量更新或删除的结果不同步。

The persistence context is not synchronized with the result of the bulk update or delete.

执行批量更新或删除操作时应该小心,因为它们可能导致数据库与活动持久性上下文中的实体之间出现不一致。通常,批量更新和删除操作应仅在新持久性上下文中的事务中执行,或者在获取或访问其状态可能受此类操作影响的实体之前执行。

这意味着,如果您需要查看此类操作的更改,您需要执行以下操作:

This means, that if you need to see the changes of such an operation you need to do the following things:


  1. 在此操作后清除 EntityManager Spring Data JPA的 @Modifying annotation有一个 clearAutomatically 标志默认为 false 。如果将其设置为true,则调用查询方法将自动清除 EntityManager (顾名思义。请谨慎使用,因为它会有效地删除所有待处理的尚未刷新到数据库的更改!

  2. EntityManager重新获取实体的新实例在存储库上调用 findOne(...)似乎是一种合理的方法,因为这大致转换为 EntityManager .find(...)。请注意,这可能仍会遇到在持久性提供程序上配置的二级缓存。

  1. Clear the EntityManager after this operation. Spring Data JPA's @Modifying annotation has a clearAutomatically flag defaulting to false. If that is set to true, invoking the query method will clear the EntityManager automatically (as the name suggests. Use that with caution, as it will effectively drop all pending changes that have not been flushed to the database yet!
  2. Re-obtain a fresh instance of the entity from the EntityManager. Calling findOne(…) on the repository seems like a reasonable way to do this as this roughly translates into EntityManager.find(…). Be aware that this might still hit 2nd level caches configured on the persistence provider.

解决这个问题最安全的方法是 - 正如规范建议的那样 - 仅对批量操作使用更新查询,并且默认情况下回退到加载实体,更改,合并方法。

The safest way to work around this is - as the spec suggests - to use update queries only for bulk operations and fall back to the "load entity, alter, merge" approach by default.

这篇关于为什么我看不到使用Spring Data JPA通过更新查询发出的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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