-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit flush needed in 3.13.0 spring-data-jpa when running @Query
that updates foreign key column with newly saved associated entity
#42322
Comments
/cc @geoand (spring) |
/cc @yrodiere |
@gsmet looking into it |
The update query was triggering a flush only because the default flush mode was set to ALWAYS but this has been changed by #41284 so Hibernate executes a flush after a query only when the result of the query may be affected by dirty state not synchronised with the db flush on JPQL/HQL query and in this case
the executed query |
I noticed that spring offers this annotation
works |
@dreab8 thanks for your analysis. So basically you are saying that Hibernate ORM is not meant to detect this case, hence no bug in Hibernate, right? |
yes this is not a bug, hibernate flushes the session only if fruit entity is dirty (e.g |
I think I know why. I'll try to come up with a PR soonish. |
So, I'm under the impression that this issue is to be closed as won't fix. Any objections? |
Agreed, since the problem was not quite the one described in the issue. Thanks for the fix. |
Hi @yrodiere,
To avoid confusion: I've never used So what I'm trying to say is that with Quarkus 3.13.0 I need an explicit flush, in one way or the other. |
Thanks for the clarification. So your patch addresses Can you confirm you don't need any other change? I.e. that migration notes sort of address your problem, which was only that your application was relying on flushes that were never meant to happen? |
Of course it would be nice if Hibernate would detect that special case automatically, but I'm ok with the explicit flushes. :-) |
Right, by "other changes" I meant "changes in Quarkus or bugfixes in Hibernate". Filing feature requests to Hibernate ORM is still an option, of course :) Anyway, thank you for confirming. |
Describe the bug
Starting from 3.13.0.CR1 (#41284 respectively), an explicit flush is needed to make sure that a
@Query('update A set associatedB where ...')
repository method does not run into a foreign key constraint violation when the other entity (here:B
) was just created in the same transaction (shortly before the update).In other words, the following fails in 3.13.0 (but works in 3.12):
B b = new B(...)
BRepository.save(b)
ARepository.updateAssociatedB(b)
<- foreign key constraint violations (b doesn't exist)Using
BRepository.saveAndFlush(b)
works around the issue.Expected behavior
No explicit flush needed; persistence layer/Hibernate should detect on its own that a flush is needed.
Actual behavior
Explicit flush required.
How to Reproduce?
quarkusio/quarkus-quickstarts@main...famod:quarkus-quickstarts:spring-jpa-flush
spring-jpa-flush
branch./mvnw clean install -f spring-data-jpa-quickstart
./mvnw clean install -f spring-data-jpa-quickstart -Dquarkus.platform.version=3.12.3
Output of
uname -a
orver
No response
Output of
java -version
No response
Quarkus version or git rev
3.13.0
Build tool (ie. output of
mvnw --version
orgradlew --version
)Maven 3.9.8
Additional information
First reported here: #41284 (comment)
I haven't had the time yet to report the issue upstream via
QuarkusLikeORMUnitTestCase
.The text was updated successfully, but these errors were encountered: