@@ -643,31 +643,27 @@ private boolean startsWithSelect() {
643
643
644
644
@ Override
645
645
protected void prepareForExecution () {
646
- if ( getSynchronizedQuerySpaces () != null && !getSynchronizedQuerySpaces ().isEmpty () ) {
647
- // The application defined query spaces on the Hibernate NativeQuery
648
- // which means the query will already perform a partial flush
649
- // according to the defined query spaces, no need to do a full flush.
650
- return ;
651
- }
652
-
653
- // otherwise we need to flush. the query itself is not required to execute
654
- // in a transaction; if there is no transaction, the flush would throw a
655
- // TransactionRequiredException which would potentially break existing
656
- // apps, so we only do the flush if a transaction is in progress.
657
- //
658
- // NOTE : this was added for JPA initially. Perhaps we want to only do
659
- // this from JPA usage?
660
- if ( shouldFlush () ) {
661
- getSession ().flush ();
646
+ if ( getSynchronizedQuerySpaces () == null || getSynchronizedQuerySpaces ().isEmpty () ) {
647
+ // We need to flush. The query itself is not required to execute in a
648
+ // transaction; if there is no transaction, the flush would throw a
649
+ // TransactionRequiredException which would potentially break existing
650
+ // apps, so we only do the flush if a transaction is in progress.
651
+ if ( shouldFlush () ) {
652
+ getSession ().flush ();
653
+ }
654
+ // Reset the callback before every execution
655
+ callback = null ;
662
656
}
663
- // Reset the callback before every execution
664
- callback = null ;
657
+ // Otherwise, the application specified query spaces via the Hibernate
658
+ // SynchronizeableQuery and so the query will already perform a partial
659
+ // flush according to the defined query spaces - no need for a full flush.
665
660
}
666
661
667
662
private boolean shouldFlush () {
668
663
if ( getSession ().isTransactionInProgress () ) {
669
664
final FlushMode flushMode = getQueryOptions ().getFlushMode ();
670
665
return switch ( flushMode == null ? getSession ().getHibernateFlushMode () : flushMode ) {
666
+ // The JPA spec requires that we auto-flush before native queries
671
667
case AUTO -> getSession ().getFactory ().getSessionFactoryOptions ().isJpaBootstrap ();
672
668
case ALWAYS -> true ;
673
669
default -> false ;
0 commit comments