进行任何查询之前,JPA自动刷新 [英] JPA auto flush before any query

查看:94
本文介绍了进行任何查询之前,JPA自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从JPA文档中,我可以看到AUTO是默认的刷新模式,刷新应在执行任何查询之前进行.我已经在spring boot jpa上进行了尝试,我可以看到刷新不会发生在来自不同实体的查询中,这是预期的吗?即使其他实体可能与此有关系(部门<->这里的人)

From JPA documentation I can see that the AUTO is the default flush mode, flush should happen before any query execution. I have tried this on spring boot jpa and I can see the flush won't happen on queries from different entities , is that expected ? even though different entity may have relation to it ( Department <--> Person here )

在根据本文进行任何查询之前,应先触发冲洗: https://vladmihalcea.com/how -jpa和休眠定义自动刷新模式/

The flush should trigger before any query according to this article : https://vladmihalcea.com/how-do-jpa-and-hibernate-define-the-auto-flush-mode/

// this triggers flush //
 Person person = personRepository.findById(5L).get();
 person.setName("hello test");
 Person person1 = (Person) entityManager.createQuery("select person from Person 
 person where person.id=11").getSingleResult(); // flush before query



// this doesn't trigger flush even if  the department has the person //
 Person person = personRepository.findById(5L).get();
 person.setName("hello test");
 Department department= (Department) entityManager.createQuery("select 
 department from Department
department where department.id=1").getSingleResult();


更新:

我注意到只有在具有DML的同一张表上的JPQL查询才会发生刷新,而对于本地sql查询,如果之前存在DML,它将始终在任何查询之前刷新.即使未发生刷新,JPQL也会返回修改后的受管实体,而不是DB中的那个.任何人都可以解释一下是否遵循JPA标准吗?

I noticed the flush happens for JPQL queries on the same table only that has the DML , while for native sql queries it will always flush before any query if there is DML before. even though no flush happens the JPQL return the managed entity with modification not the one in DB. can anyone please explain if this follow JPA standard or not ?

推荐答案

由于JPA是一种规范,因此此问题很容易回答.查看规格:-)

As JPA is a specification this question is simple to answer. Check out the spec :-)

3.10.8查询和刷新模式

刷新模式设置会影响查询结果,如下所示. 在事务内执行查询时,如果在Query,TypedQuery或StoredProcedureQuery对象上设置了FlushModeType.AUTO,或者持久性上下文的刷新模式设置为AUTO(默认值),并且尚未为在查询对象中,持久性提供者负责确保对持久性上下文中所有实体状态的所有更新都可能对查询的处理可见,这些更新可能潜在地影响查询的结果. 询问.持久性提供程序实现可以通过将这些实体刷新到数据库或通过其他某种方式来实现.如果设置了FlushModeType.COMMIT,则未指定对持久化上下文中的实体进行查询更新的效果.

The flush mode setting affects the result of a query as follows. When queries are executed within a transaction, if FlushModeType.AUTO is set on the Query, TypedQuery, or StoredProcedureQuery object, or if the flush mode setting for the persistence context is AUTO (the default) and a flush mode setting has not been specified for the query object, the persistence provider is responsible for ensuring that all updates to the state of all entities in the persistence context which could potentially affect the result of the query are visible to the processing of the query. The persistence provider implementation may achieve this by flushing those entities to the database or by some other means. If FlushModeType.COMMIT is set, the effect of updates made to entities in the persistence context upon queries is unspecified.

如果持久性上下文尚未加入当前事务,则持久性提供程序必须 无论刷新模式设置如何,都不会刷新到数据库.

If the persistence context has not been joined to the current transaction, the persistence provider must not flush to the database regardless of the flush mode setting.

package javax.persistence;
public enum FlushModeType {
COMMIT,
AUTO
}

如果没有活动的事务,则持久性提供程序不得刷新到数据库

If there is no transaction active, the persistence provider must not flush to the database

https ://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam = 1561799350_4cc62583442da694a6a033af82faf986

然后是Hibernate Doc:

6.1.自动冲洗

默认情况下,Hibernate使用AUTO刷新模式,该模式在以下情况下触发刷新:

By default, Hibernate uses the AUTO flush mode which triggers a flush in the following circumstances:

  • 在提交交易之前

  • prior to committing a Transaction

在执行与排队的实体操作重叠的JPQL/HQL查询之前

prior to executing a JPQL/HQL query that overlaps with the queued entity actions

在执行任何未注册同步的本机SQL查询之前

before executing any native SQL query that has no registered synchronization

https://docs.jboss.org /hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#flushing

这篇关于进行任何查询之前,JPA自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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