Skip to content

Commit 4027f13

Browse files
committed
HHH-8487 - Auto-flush on JPA native SQL query
1 parent c6fbd4b commit 4027f13

File tree

1 file changed

+8
-2
lines changed
  • hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal

1 file changed

+8
-2
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/QueryImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@
5050
import org.hibernate.HibernateException;
5151
import org.hibernate.LockMode;
5252
import org.hibernate.SQLQuery;
53+
import org.hibernate.Session;
5354
import org.hibernate.TypeMismatchException;
5455
import org.hibernate.engine.query.spi.NamedParameterDescriptor;
5556
import org.hibernate.engine.query.spi.OrdinalParameterDescriptor;
5657
import org.hibernate.engine.query.spi.ParameterMetadata;
5758
import org.hibernate.engine.spi.SessionFactoryImplementor;
59+
import org.hibernate.engine.spi.SessionImplementor;
5860
import org.hibernate.hql.internal.QueryExecutionRequestException;
5961
import org.hibernate.internal.SQLQueryImpl;
6062
import org.hibernate.jpa.AvailableSettings;
@@ -416,8 +418,12 @@ private void beforeQuery() {
416418
return;
417419
}
418420

419-
// otherwise we need to flush
420-
getEntityManager().flush();
421+
// otherwise we need to flush. the query itself is not required to execute in a transaction; if there is
422+
// no transaction, the flush would throw a TransactionRequiredException which would potentially break existing
423+
// apps, so we only do the flush if a transaction is in progress.
424+
if ( getEntityManager().isTransactionInProgress() ) {
425+
getEntityManager().flush();
426+
}
421427
}
422428

423429
@Override

0 commit comments

Comments
 (0)