spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.93k stars 1.39k forks source link

Missing implicite entityManager.flush() before storedprocedure call and Spring-Data Repository #3433

Closed ere-one closed 2 months ago

ere-one commented 2 months ago

I have an Spring Data JPA Repository with a StoreProcedure caller method, annotated with @Procedure. I made some changes on entities which are attached to the entityManager. If i call the StoredProcedure method without explicitly calling entityManager.flush() first, the StoredProcedure does not see the changes.

Issue is also described here: https://stackoverflow.com/questions/53839795/how-to-flush-spring-data-jpa-before-calling-stored-procedure

christophstrobl commented 2 months ago

Thanks for getting in touch. We prefer to use GitHub issues only for bugs and enhancements. The question on SO seems to have been answered already.

ere-one commented 2 months ago

Actually it is a bug in the Sppring Data JPA Repository. Because entityManager.flush() must be called implicitally by a vendor if FlushModeType.AUTO is used, see related JEE API Doc: https://docs.oracle.com/javaee%2F7%2Fapi%2F%2F/javax/persistence/FlushModeType.html

"... 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. ..."

In case of a Spring-Data JPA Repository methods annotated with @Query, the entityManager.flush() will be called implicitally before executing the query, so the query can use/see the changes made in the same transaction. In case of a Spring-Data JPA Repository methods annotated with @Procedure the entityManager.flush() will not be called implicitally, so the stored procedure, in which queries may be used, will not use/see changes made in the same transaction.

We use Spring-Boot 2.7.10