HHH-16492 Hibernate 6 does not auto flush when calling Query.stream() with NativeQuery

This commit is contained in:
Andrea Boriero 2023-04-24 13:30:57 +02:00 committed by Andrea Boriero
parent 309cafbf93
commit 99f45f042e
4 changed files with 19 additions and 60 deletions

View File

@ -385,7 +385,9 @@ public abstract class AbstractSelectionQuery<R>
protected void beforeQuery() {
getQueryParameterBindings().validate();
getSession().prepareForQueryExecution(false);
getSession().prepareForQueryExecution(
requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() )
);
prepareForExecution();
assert sessionFlushMode == null;
@ -407,6 +409,14 @@ public abstract class AbstractSelectionQuery<R>
protected abstract void prepareForExecution();
protected void afterQuery(boolean success) {
afterQuery();
if ( !getSession().isTransactionInProgress() ) {
getSession().getJdbcCoordinator().getLogicalConnection().afterTransaction();
}
getSession().afterOperation( success );
}
protected void afterQuery() {
if ( sessionFlushMode != null ) {
getSession().setHibernateFlushMode( sessionFlushMode );
sessionFlushMode = null;
@ -415,10 +425,6 @@ public abstract class AbstractSelectionQuery<R>
getSession().setCacheMode( sessionCacheMode );
sessionCacheMode = null;
}
if ( !getSession().isTransactionInProgress() ) {
getSession().getJdbcCoordinator().getLogicalConnection().afterTransaction();
}
getSession().afterOperation( success );
}
protected boolean requiresTxn(LockMode lockMode) {
@ -434,7 +440,13 @@ public abstract class AbstractSelectionQuery<R>
@Override
public ScrollableResultsImplementor<R> scroll(ScrollMode scrollMode) {
return doScroll( scrollMode );
beforeQuery();
try {
return doScroll( scrollMode );
}
finally {
afterQuery();
}
}
protected abstract ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode);

View File

@ -13,9 +13,7 @@ import java.util.List;
import java.util.Set;
import org.hibernate.ScrollMode;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.EmptyScrollableResults;
import org.hibernate.query.results.ResultSetMapping;
import org.hibernate.query.spi.DomainQueryExecutionContext;
@ -96,10 +94,7 @@ public class NativeSelectQueryPlanImpl<R> implements NativeSelectQueryPlan<R> {
affectedTableNames
);
final SharedSessionContractImplementor session = executionContext.getSession();
final SessionFactoryImplementor factory = session.getFactory();
final JdbcServices jdbcServices = factory.getJdbcServices();
return jdbcServices.getJdbcSelectExecutor().list(
return executionContext.getSession().getJdbcServices().getJdbcSelectExecutor().list(
jdbcSelect,
jdbcParameterBindings,
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext ),

View File

@ -16,9 +16,7 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
@ -508,7 +506,6 @@ public class QuerySqmImpl<R>
protected List<R> doList() {
verifySelect();
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches() || AppliedGraphs.containsCollectionFetches(
@ -629,7 +626,6 @@ public class QuerySqmImpl<R>
@Override
protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) {
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
return resolveSelectQueryPlan().performScroll( scrollMode, this );
}
@ -1076,46 +1072,6 @@ public class QuerySqmImpl<R>
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// select execution
@Override
public List<R> list() {
return super.list();
}
@Override
public ScrollableResultsImplementor<R> scroll() {
return super.scroll();
}
@Override
public ScrollableResultsImplementor<R> scroll(ScrollMode scrollMode) {
return super.scroll( scrollMode );
}
@Override
public Stream<R> stream() {
//noinspection unchecked
return super.stream();
}
@Override
public R uniqueResult() {
return super.uniqueResult();
}
@Override
public R getSingleResult() {
return super.getSingleResult();
}
@Override
public Optional<R> uniqueResultOptional() {
return super.uniqueResultOptional();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Named query externalization

View File

@ -256,8 +256,6 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
}
protected List<R> doList() {
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
final boolean hasLimit = hasLimit( sqmStatement, getQueryOptions() );
@ -330,8 +328,6 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R> implemen
@Override
protected ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode) {
getSession().prepareForQueryExecution( requiresTxn( getQueryOptions().getLockOptions().findGreatestLockMode() ) );
return resolveQueryPlan().performScroll( scrollMode, this );
}