HHH-16931 add SelectionQuery.getResultCount()

This commit is contained in:
Gavin King 2024-02-19 12:53:55 +01:00
parent 7f14b2cbf8
commit 1180be0a0f
17 changed files with 545 additions and 168 deletions

View File

@ -943,7 +943,7 @@ public class ProcedureCallImpl<R>
try { try {
final Output rtn = outputs().getCurrent(); final Output rtn = outputs().getCurrent();
if ( !(rtn instanceof ResultSetOutput) ) { if ( !(rtn instanceof ResultSetOutput) ) {
throw new IllegalStateException( "Current CallableStatement ou was not a ResultSet, but getResultList was called" ); throw new IllegalStateException( "Current CallableStatement was not a ResultSet, but getResultList was called" );
} }
//noinspection unchecked //noinspection unchecked
@ -964,14 +964,19 @@ public class ProcedureCallImpl<R>
} }
} }
@Override
public long getResultCount() {
throw new UnsupportedOperationException( "getResultCount() not implemented for ProcedureCall/StoredProcedureQuery" );
}
@Override @Override
public ScrollableResultsImplementor<R> scroll(ScrollMode scrollMode) { public ScrollableResultsImplementor<R> scroll(ScrollMode scrollMode) {
throw new UnsupportedOperationException( "Query#scroll is not valid for ProcedureCall/StoredProcedureQuery" ); throw new UnsupportedOperationException( "scroll() is not implemented for ProcedureCall/StoredProcedureQuery" );
} }
@Override @Override
protected ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode) { protected ScrollableResultsImplementor<R> doScroll(ScrollMode scrollMode) {
throw new UnsupportedOperationException( "Query#scroll is not valid for ProcedureCall/StoredProcedureQuery" ); throw new UnsupportedOperationException( "scroll() is not implemented for ProcedureCall/StoredProcedureQuery" );
} }
@Override @Override

View File

@ -207,6 +207,19 @@ public interface SelectionQuery<R> extends CommonQueryContract {
*/ */
Optional<R> uniqueResultOptional(); Optional<R> uniqueResultOptional();
/**
* Determine the size of the query result list that would be
* returned by calling {@link #getResultList()} with no
* {@linkplain #getFirstResult() offset} or
* {@linkplain #getMaxResults() limit} applied to the query.
*
* @return the size of the list that would be returned
*
* @since 6.5
*/
@Incubating
long getResultCount();
SelectionQuery<R> setHint(String hintName, Object value); SelectionQuery<R> setHint(String hintName, Object value);
/** /**

View File

@ -8,15 +8,12 @@ package org.hibernate.query.hql.internal;
import java.util.Set; import java.util.Set;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.model.domain.EntityDomainType; import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.sqm.internal.SimpleSqmCopyContext;
import org.hibernate.query.sqm.tree.SqmCopyContext; import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmDeleteOrUpdateStatement; import org.hibernate.query.sqm.tree.SqmDeleteOrUpdateStatement;
import org.hibernate.query.sqm.tree.SqmStatement; import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement; import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
import org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor; import org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.from.SqmRoot; import org.hibernate.query.sqm.tree.from.SqmRoot;
import org.hibernate.query.sqm.tree.select.SqmQueryGroup; import org.hibernate.query.sqm.tree.select.SqmQueryGroup;
import org.hibernate.query.sqm.tree.select.SqmQueryPart; import org.hibernate.query.sqm.tree.select.SqmQueryPart;
@ -31,20 +28,21 @@ import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
*/ */
public class QuerySplitter { public class QuerySplitter {
public static <R> SqmSelectStatement<R>[] split( public static <R> SqmSelectStatement<R>[] split(SqmSelectStatement<R> statement) {
SqmSelectStatement<R> statement,
SessionFactoryImplementor sessionFactory) {
// We only allow unmapped polymorphism in a very restricted way. Specifically, // We only allow unmapped polymorphism in a very restricted way. Specifically,
// the unmapped polymorphic reference can only be a root and can be the only // the unmapped polymorphic reference can only be a root and can be the only
// root. Use that restriction to locate the unmapped polymorphic reference // root. Use that restriction to locate the unmapped polymorphic reference
final SqmRoot<?> unmappedPolymorphicReference = findUnmappedPolymorphicReference( statement.getQueryPart() ); final SqmRoot<?> unmappedPolymorphicReference = findUnmappedPolymorphicReference( statement.getQueryPart() );
if ( unmappedPolymorphicReference == null ) { if ( unmappedPolymorphicReference == null ) {
return new SqmSelectStatement[] { statement }; @SuppressWarnings("unchecked")
SqmSelectStatement<R>[] sqmSelectStatement = new SqmSelectStatement[] { statement };
return sqmSelectStatement;
} }
final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource(); final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors(); final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors();
@SuppressWarnings("unchecked")
final SqmSelectStatement<R>[] expanded = new SqmSelectStatement[ implementors.size() ]; final SqmSelectStatement<R>[] expanded = new SqmSelectStatement[ implementors.size() ];
int i = 0; int i = 0;
@ -97,20 +95,21 @@ public class QuerySplitter {
return (S) statement.copy( context ); return (S) statement.copy( context );
} }
public static <R> SqmDeleteStatement<R>[] split( public static <R> SqmDeleteStatement<R>[] split(SqmDeleteStatement<R> statement) {
SqmDeleteStatement<R> statement,
SessionFactoryImplementor sessionFactory) {
// We only allow unmapped polymorphism in a very restricted way. Specifically, // We only allow unmapped polymorphism in a very restricted way. Specifically,
// the unmapped polymorphic reference can only be a root and can be the only // the unmapped polymorphic reference can only be a root and can be the only
// root. Use that restriction to locate the unmapped polymorphic reference // root. Use that restriction to locate the unmapped polymorphic reference
final SqmRoot<?> unmappedPolymorphicReference = findUnmappedPolymorphicReference( statement ); final SqmRoot<?> unmappedPolymorphicReference = findUnmappedPolymorphicReference( statement );
if ( unmappedPolymorphicReference == null ) { if ( unmappedPolymorphicReference == null ) {
return new SqmDeleteStatement[] { statement }; @SuppressWarnings("unchecked")
SqmDeleteStatement<R>[] sqmDeleteStatement = new SqmDeleteStatement[] { statement };
return sqmDeleteStatement;
} }
final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource(); final SqmPolymorphicRootDescriptor<?> unmappedPolymorphicDescriptor = (SqmPolymorphicRootDescriptor<?>) unmappedPolymorphicReference.getReferencedPathSource();
final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors(); final Set<EntityDomainType<?>> implementors = unmappedPolymorphicDescriptor.getImplementors();
@SuppressWarnings("unchecked")
final SqmDeleteStatement<R>[] expanded = new SqmDeleteStatement[ implementors.size() ]; final SqmDeleteStatement<R>[] expanded = new SqmDeleteStatement[ implementors.size() ];
int i = 0; int i = 0;
@ -125,6 +124,8 @@ public class QuerySplitter {
if ( queryPart.getTarget().getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?> ) { if ( queryPart.getTarget().getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?> ) {
return queryPart.getTarget(); return queryPart.getTarget();
} }
else {
return null; return null;
} }
} }
}

View File

@ -50,6 +50,7 @@ import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter; import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer; import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer; import org.hibernate.query.TupleTransformer;
import org.hibernate.query.internal.DelegatingDomainQueryExecutionContext;
import org.hibernate.query.internal.ParameterMetadataImpl; import org.hibernate.query.internal.ParameterMetadataImpl;
import org.hibernate.query.internal.QueryOptionsImpl; import org.hibernate.query.internal.QueryOptionsImpl;
import org.hibernate.query.internal.QueryParameterBindingsImpl; import org.hibernate.query.internal.QueryParameterBindingsImpl;
@ -58,7 +59,9 @@ import org.hibernate.query.named.NamedResultSetMappingMemento;
import org.hibernate.query.results.Builders; import org.hibernate.query.results.Builders;
import org.hibernate.query.results.ResultBuilder; import org.hibernate.query.results.ResultBuilder;
import org.hibernate.query.results.ResultSetMapping; import org.hibernate.query.results.ResultSetMapping;
import org.hibernate.query.results.ResultSetMappingImpl;
import org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy; import org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy;
import org.hibernate.query.results.dynamic.DynamicResultBuilderBasicStandard;
import org.hibernate.query.results.dynamic.DynamicResultBuilderEntityCalculated; import org.hibernate.query.results.dynamic.DynamicResultBuilderEntityCalculated;
import org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard; import org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard;
import org.hibernate.query.results.dynamic.DynamicResultBuilderInstantiation; import org.hibernate.query.results.dynamic.DynamicResultBuilderInstantiation;
@ -71,6 +74,7 @@ import org.hibernate.query.spi.NonSelectQueryPlan;
import org.hibernate.query.spi.ParameterMetadataImplementor; import org.hibernate.query.spi.ParameterMetadataImplementor;
import org.hibernate.query.spi.QueryEngine; import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.spi.QueryInterpretationCache; import org.hibernate.query.spi.QueryInterpretationCache;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.spi.QueryParameterBinding; import org.hibernate.query.spi.QueryParameterBinding;
import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.query.spi.QueryParameterImplementor; import org.hibernate.query.spi.QueryParameterImplementor;
@ -87,6 +91,7 @@ import org.hibernate.query.sql.spi.SelectInterpretationsKey;
import org.hibernate.sql.exec.internal.CallbackImpl; import org.hibernate.sql.exec.internal.CallbackImpl;
import org.hibernate.sql.exec.spi.Callback; import org.hibernate.sql.exec.spi.Callback;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer; import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer;
import org.hibernate.sql.results.spi.SingleResultConsumer;
import org.hibernate.transform.ResultTransformer; import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.BasicTypeReference; import org.hibernate.type.BasicTypeReference;
@ -621,6 +626,17 @@ public class NativeQueryImpl<R>
return resolveSelectQueryPlan().performList( this ); return resolveSelectQueryPlan().performList( this );
} }
@Override
public long getResultCount() {
final DelegatingDomainQueryExecutionContext context = new DelegatingDomainQueryExecutionContext(this) {
@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}
};
return createCountQueryPlan().executeQuery( context, new SingleResultConsumer<>() );
}
protected SelectQueryPlan<R> resolveSelectQueryPlan() { protected SelectQueryPlan<R> resolveSelectQueryPlan() {
if ( isCacheableQuery() ) { if ( isCacheableQuery() ) {
final QueryInterpretationCache.Key cacheKey = generateSelectInterpretationsKey( resultSetMapping ); final QueryInterpretationCache.Key cacheKey = generateSelectInterpretationsKey( resultSetMapping );
@ -647,7 +663,7 @@ public class NativeQueryImpl<R>
@Override @Override
public List<ParameterOccurrence> getQueryParameterOccurrences() { public List<ParameterOccurrence> getQueryParameterOccurrences() {
return NativeQueryImpl.this.parameterOccurrences; return parameterOccurrences;
} }
@Override @Override
@ -665,6 +681,42 @@ public class NativeQueryImpl<R>
.createQueryPlan( queryDefinition, getSessionFactory() ); .createQueryPlan( queryDefinition, getSessionFactory() );
} }
private NativeSelectQueryPlan<Long> createCountQueryPlan() {
final BasicType<Long> longType = getSessionFactory().getTypeConfiguration().getBasicTypeForJavaType(Long.class);
final String sqlString = expandParameterLists();
final NativeSelectQueryDefinition<Long> queryDefinition = new NativeSelectQueryDefinition<>() {
@Override
public String getSqlString() {
return "select count(*) from (" + sqlString + ") a_";
}
@Override
public boolean isCallable() {
return false;
}
@Override
public List<ParameterOccurrence> getQueryParameterOccurrences() {
return parameterOccurrences;
}
@Override
public ResultSetMapping getResultSetMapping() {
final ResultSetMappingImpl mapping = new ResultSetMappingImpl( "", true );
mapping.addResultBuilder( new DynamicResultBuilderBasicStandard( 1, longType ) );
return mapping;
}
@Override
public Set<String> getAffectedTableNames() {
return querySpaces;
}
};
return getSessionFactory().getQueryEngine().getNativeQueryInterpreter()
.createQueryPlan( queryDefinition, getSessionFactory() );
}
protected String expandParameterLists() { protected String expandParameterLists() {
if ( parameterOccurrences == null || parameterOccurrences.isEmpty() ) { if ( parameterOccurrences == null || parameterOccurrences.isEmpty() ) {
return sqlString; return sqlString;

View File

@ -111,6 +111,7 @@ import jakarta.persistence.LockModeType;
import jakarta.persistence.Parameter; import jakarta.persistence.Parameter;
import jakarta.persistence.PersistenceException; import jakarta.persistence.PersistenceException;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import org.hibernate.sql.results.spi.SingleResultConsumer;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE; import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE;
@ -131,6 +132,7 @@ import static org.hibernate.query.sqm.internal.SqmUtil.isSelect;
import static org.hibernate.query.sqm.internal.SqmUtil.sortSpecification; import static org.hibernate.query.sqm.internal.SqmUtil.sortSpecification;
import static org.hibernate.query.sqm.internal.SqmUtil.verifyIsNonSelectStatement; import static org.hibernate.query.sqm.internal.SqmUtil.verifyIsNonSelectStatement;
import static org.hibernate.query.sqm.internal.TypecheckUtil.assertAssignable; import static org.hibernate.query.sqm.internal.TypecheckUtil.assertAssignable;
import static org.hibernate.query.sqm.tree.SqmCopyContext.noParamCopyContext;
/** /**
* {@link Query} implementation based on an SQM * {@link Query} implementation based on an SQM
@ -490,6 +492,20 @@ public class QuerySqmImpl<R>
} }
} }
@Override
public long getResultCount() {
verifySelect();
final DelegatingDomainQueryExecutionContext context = new DelegatingDomainQueryExecutionContext(this) {
@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}
};
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
return buildConcreteSelectQueryPlan( sqmStatement.createCountQuery(), Long.class, null, getQueryOptions() )
.executeQuery( context, new SingleResultConsumer<>() );
}
protected List<R> doList() { protected List<R> doList() {
verifySelect(); verifySelect();
@ -613,39 +629,40 @@ public class QuerySqmImpl<R>
} }
private SelectQueryPlan<R> buildSelectQueryPlan() { private SelectQueryPlan<R> buildSelectQueryPlan() {
final SqmSelectStatement<R>[] concreteSqmStatements = QuerySplitter.split( final SqmSelectStatement<R>[] concreteSqmStatements =
(SqmSelectStatement<R>) getSqmStatement(), QuerySplitter.split( (SqmSelectStatement<R>) getSqmStatement() );
getSession().getFactory()
);
if ( concreteSqmStatements.length > 1 ) { if ( concreteSqmStatements.length > 1 ) {
return buildAggregatedSelectQueryPlan( concreteSqmStatements ); return buildAggregatedSelectQueryPlan( concreteSqmStatements );
} }
else { else {
return buildConcreteSelectQueryPlan( concreteSqmStatements[0], getResultType(), getQueryOptions() ); return buildConcreteSelectQueryPlan( concreteSqmStatements[0] );
} }
} }
private SelectQueryPlan<R> buildAggregatedSelectQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) { private SelectQueryPlan<R> buildAggregatedSelectQueryPlan(SqmSelectStatement<R>[] concreteSqmStatements) {
//noinspection unchecked @SuppressWarnings("unchecked")
final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ]; final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ];
// todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level // todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level
for ( int i = 0, x = concreteSqmStatements.length; i < x; i++ ) { for ( int i = 0, x = concreteSqmStatements.length; i < x; i++ ) {
aggregatedQueryPlans[i] = buildConcreteSelectQueryPlan( aggregatedQueryPlans[i] = buildConcreteSelectQueryPlan( concreteSqmStatements[i] );
concreteSqmStatements[i], }
return new AggregatedSelectQueryPlanImpl<>( aggregatedQueryPlans );
}
private SelectQueryPlan<R> buildConcreteSelectQueryPlan(SqmSelectStatement<R> concreteSqmStatement) {
return buildConcreteSelectQueryPlan(
concreteSqmStatement,
getResultType(), getResultType(),
tupleMetadata,
getQueryOptions() getQueryOptions()
); );
} }
return new AggregatedSelectQueryPlanImpl<>( aggregatedQueryPlans );
}
private <T> SelectQueryPlan<T> buildConcreteSelectQueryPlan( private <T> SelectQueryPlan<T> buildConcreteSelectQueryPlan(
SqmSelectStatement<?> concreteSqmStatement, SqmSelectStatement<T> concreteSqmStatement,
Class<T> resultType, Class<T> resultType,
TupleMetadata tupleMetadata,
QueryOptions queryOptions) { QueryOptions queryOptions) {
return new ConcreteSqmSelectQueryPlan<>( return new ConcreteSqmSelectQueryPlan<>(
concreteSqmStatement, concreteSqmStatement,
@ -742,11 +759,8 @@ public class QuerySqmImpl<R>
} }
private NonSelectQueryPlan buildDeleteQueryPlan() { private NonSelectQueryPlan buildDeleteQueryPlan() {
final SqmDeleteStatement<R>[] concreteSqmStatements = QuerySplitter.split( final SqmDeleteStatement<R>[] concreteSqmStatements =
(SqmDeleteStatement<R>) getSqmStatement(), QuerySplitter.split( (SqmDeleteStatement<R>) getSqmStatement() );
getSessionFactory()
);
return concreteSqmStatements.length > 1 return concreteSqmStatements.length > 1
? buildAggregatedDeleteQueryPlan( concreteSqmStatements ) ? buildAggregatedDeleteQueryPlan( concreteSqmStatements )
: buildConcreteDeleteQueryPlan( concreteSqmStatements[0] ); : buildConcreteDeleteQueryPlan( concreteSqmStatements[0] );
@ -948,7 +962,7 @@ public class QuerySqmImpl<R>
@Override @Override
public Query<R> setOrder(List<Order<? super R>> orderList) { public Query<R> setOrder(List<Order<? super R>> orderList) {
if ( sqm instanceof SqmSelectStatement ) { if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( noParamCopyContext() );
final SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm; final SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
select.orderBy( orderList.stream().map( order -> sortSpecification( select, order ) ) select.orderBy( orderList.stream().map( order -> sortSpecification( select, order ) )
.collect( toList() ) ); .collect( toList() ) );
@ -965,7 +979,7 @@ public class QuerySqmImpl<R>
@Override @Override
public Query<R> setOrder(Order<? super R> order) { public Query<R> setOrder(Order<? super R> order) {
if ( sqm instanceof SqmSelectStatement ) { if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() ); sqm = sqm.copy( noParamCopyContext() );
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm; SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;
select.orderBy( sortSpecification( select, order ) ); select.orderBy( sortSpecification( select, order ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries, // TODO: when the QueryInterpretationCache can handle caching criteria queries,

View File

@ -66,6 +66,7 @@ import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection; import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.sql.results.internal.TupleMetadata; import org.hibernate.sql.results.internal.TupleMetadata;
import org.hibernate.sql.results.spi.ResultsConsumer; import org.hibernate.sql.results.spi.ResultsConsumer;
import org.hibernate.sql.results.spi.SingleResultConsumer;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
@ -131,7 +132,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
else { else {
final SqmSelection<?> selection = selections.get(0); final SqmSelection<?> selection = selections.get(0);
if ( selection!=null ) { if ( selection!=null ) {
JavaType<?> javaType = selection.getNodeJavaType(); final JavaType<?> javaType = selection.getNodeJavaType();
if ( javaType != null) { if ( javaType != null) {
return javaType.getJavaTypeClass(); return javaType.getJavaTypeClass();
} }
@ -317,6 +318,18 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
resetCallback(); resetCallback();
} }
@Override
public long getResultCount() {
final DelegatingDomainQueryExecutionContext context = new DelegatingDomainQueryExecutionContext(this) {
@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}
};
return buildConcreteQueryPlan( getSqmStatement().createCountQuery(), Long.class, null, getQueryOptions() )
.executeQuery( context, new SingleResultConsumer<>() );
}
protected List<R> doList() { protected List<R> doList() {
final SqmSelectStatement<?> sqmStatement = getSqmStatement(); final SqmSelectStatement<?> sqmStatement = getSqmStatement();
final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches(); final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
@ -413,17 +426,13 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
} }
private SelectQueryPlan<R> buildQueryPlan() { private SelectQueryPlan<R> buildQueryPlan() {
final SqmSelectStatement<?>[] concreteSqmStatements = QuerySplitter.split( final SqmSelectStatement<R>[] concreteSqmStatements = QuerySplitter.split( getSqmStatement() );
(SqmSelectStatement<?>) getSqmStatement(),
getSession().getFactory()
);
return concreteSqmStatements.length > 1 return concreteSqmStatements.length > 1
? buildAggregatedQueryPlan( concreteSqmStatements ) ? buildAggregatedQueryPlan( concreteSqmStatements )
: buildConcreteQueryPlan( concreteSqmStatements[0], getQueryOptions() ); : buildConcreteQueryPlan( concreteSqmStatements[0], getQueryOptions() );
} }
private SelectQueryPlan<R> buildAggregatedQueryPlan(SqmSelectStatement<?>[] concreteSqmStatements) { private SelectQueryPlan<R> buildAggregatedQueryPlan(SqmSelectStatement<R>[] concreteSqmStatements) {
//noinspection unchecked //noinspection unchecked
final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ]; final SelectQueryPlan<R>[] aggregatedQueryPlans = new SelectQueryPlan[ concreteSqmStatements.length ];
// todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level // todo (6.0) : we want to make sure that certain thing (ResultListTransformer, etc) only get applied at the aggregator-level
@ -434,7 +443,15 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
} }
private SelectQueryPlan<R> buildConcreteQueryPlan( private SelectQueryPlan<R> buildConcreteQueryPlan(
SqmSelectStatement<?> concreteSqmStatement, SqmSelectStatement<R> concreteSqmStatement,
QueryOptions queryOptions) {
return buildConcreteQueryPlan( concreteSqmStatement, expectedResultType, tupleMetadata, queryOptions);
}
private <T> ConcreteSqmSelectQueryPlan<T> buildConcreteQueryPlan(
SqmSelectStatement<T> concreteSqmStatement,
Class<T> expectedResultType,
TupleMetadata tupleMetadata,
QueryOptions queryOptions) { QueryOptions queryOptions) {
return new ConcreteSqmSelectQueryPlan<>( return new ConcreteSqmSelectQueryPlan<>(
concreteSqmStatement, concreteSqmStatement,
@ -447,7 +464,6 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
} }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// InterpretationsKeySource // InterpretationsKeySource

View File

@ -94,6 +94,11 @@ public abstract class DelegatingSqmSelectionQueryImplementor<R> implements SqmSe
return getDelegate().getResultList(); return getDelegate().getResultList();
} }
@Override
public long getResultCount() {
return getDelegate().getResultCount();
}
@Override @Override
public ScrollableResults<R> scroll() { public ScrollableResults<R> scroll() {
return getDelegate().scroll(); return getDelegate().scroll();

View File

@ -9,7 +9,6 @@ package org.hibernate.query.sqm.tree;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier;
import org.hibernate.query.criteria.JpaQueryableCriteria; import org.hibernate.query.criteria.JpaQueryableCriteria;
import org.hibernate.query.sqm.SqmQuerySource; import org.hibernate.query.sqm.SqmQuerySource;

View File

@ -33,7 +33,7 @@ public abstract class AbstractSqmAttributeJoin<O,T>
extends AbstractSqmQualifiedJoin<O,T> extends AbstractSqmQualifiedJoin<O,T>
implements SqmAttributeJoin<O,T> { implements SqmAttributeJoin<O,T> {
private final boolean fetched; private boolean fetched;
public AbstractSqmAttributeJoin( public AbstractSqmAttributeJoin(
SqmFrom<?,O> lhs, SqmFrom<?,O> lhs,
@ -84,10 +84,16 @@ public abstract class AbstractSqmAttributeJoin<O,T>
return getJavaTypeDescriptor(); return getJavaTypeDescriptor();
} }
@Override
public boolean isFetched() { public boolean isFetched() {
return fetched; return fetched;
} }
@Override
public void clearFetched() {
fetched = false;
}
@Override @Override
public <X> X accept(SemanticQueryWalker<X> walker) { public <X> X accept(SemanticQueryWalker<X> walker) {
return walker.visitQualifiedAttributeJoin( this ); return walker.visitQualifiedAttributeJoin( this );

View File

@ -15,7 +15,7 @@ import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.hibernate.metamodel.mapping.ModelPartContainer; import org.hibernate.Internal;
import org.hibernate.metamodel.model.domain.BagPersistentAttribute; import org.hibernate.metamodel.model.domain.BagPersistentAttribute;
import org.hibernate.metamodel.model.domain.EntityDomainType; import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.metamodel.model.domain.ListPersistentAttribute; import org.hibernate.metamodel.model.domain.ListPersistentAttribute;
@ -198,42 +198,42 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
return sqmPath; return sqmPath;
} }
private ModelPartContainer findModelPartContainer(SqmAttributeJoin<?, ?> attributeJoin, SqmCreationState creationState) { // private ModelPartContainer findModelPartContainer(SqmAttributeJoin<?, ?> attributeJoin, SqmCreationState creationState) {
final SqmFrom<?, ?> lhs = attributeJoin.getLhs(); // final SqmFrom<?, ?> lhs = attributeJoin.getLhs();
if ( lhs instanceof SqmAttributeJoin<?, ?> ) { // if ( lhs instanceof SqmAttributeJoin<?, ?> ) {
final SqmAttributeJoin<?, ?> lhsAttributeJoin = (SqmAttributeJoin<?, ?>) lhs; // final SqmAttributeJoin<?, ?> lhsAttributeJoin = (SqmAttributeJoin<?, ?>) lhs;
if ( lhsAttributeJoin.getReferencedPathSource() instanceof EntityDomainType<?> ) { // if ( lhsAttributeJoin.getReferencedPathSource() instanceof EntityDomainType<?> ) {
final String entityName = ( (EntityDomainType<?>) lhsAttributeJoin.getReferencedPathSource() ).getHibernateEntityName(); // final String entityName = ( (EntityDomainType<?>) lhsAttributeJoin.getReferencedPathSource() ).getHibernateEntityName();
return (ModelPartContainer) creationState.getCreationContext() // return (ModelPartContainer) creationState.getCreationContext()
.getJpaMetamodel() // .getJpaMetamodel()
.getMappingMetamodel() // .getMappingMetamodel()
.getEntityDescriptor( entityName ) // .getEntityDescriptor( entityName )
.findSubPart( attributeJoin.getAttribute().getName(), null ); // .findSubPart( attributeJoin.getAttribute().getName(), null );
} // }
else { // else {
return (ModelPartContainer) findModelPartContainer( lhsAttributeJoin, creationState ) // return (ModelPartContainer) findModelPartContainer( lhsAttributeJoin, creationState )
.findSubPart( attributeJoin.getAttribute().getName(), null ); // .findSubPart( attributeJoin.getAttribute().getName(), null );
} // }
} // }
else { // else {
final String entityName; // final String entityName;
if ( lhs instanceof SqmRoot<?> ) { // if ( lhs instanceof SqmRoot<?> ) {
entityName = ( (SqmRoot<?>) lhs ).getEntityName(); // entityName = ( (SqmRoot<?>) lhs ).getEntityName();
} // }
else if ( lhs instanceof SqmEntityJoin<?> ) { // else if ( lhs instanceof SqmEntityJoin<?> ) {
entityName = ( (SqmEntityJoin<?>) lhs ).getEntityName(); // entityName = ( (SqmEntityJoin<?>) lhs ).getEntityName();
} // }
else { // else {
assert lhs instanceof SqmCrossJoin<?>; // assert lhs instanceof SqmCrossJoin<?>;
entityName = ( (SqmCrossJoin<?>) lhs ).getEntityName(); // entityName = ( (SqmCrossJoin<?>) lhs ).getEntityName();
} // }
return (ModelPartContainer) creationState.getCreationContext() // return (ModelPartContainer) creationState.getCreationContext()
.getJpaMetamodel() // .getJpaMetamodel()
.getMappingMetamodel() // .getMappingMetamodel()
.getEntityDescriptor( entityName ) // .getEntityDescriptor( entityName )
.findSubPart( attributeJoin.getAttribute().getName(), null ); // .findSubPart( attributeJoin.getAttribute().getName(), null );
} // }
} // }
@Override @Override
public boolean hasJoins() { public boolean hasJoins() {
@ -254,6 +254,29 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
findRoot().addOrderedJoin( join ); findRoot().addOrderedJoin( join );
} }
@Internal
public void removeLeftFetchJoins() {
if ( joins != null ) {
for ( SqmJoin<T, ?> join : new ArrayList<>(joins) ) {
if ( join instanceof SqmAttributeJoin ) {
final SqmAttributeJoin<T, ?> attributeJoin = (SqmAttributeJoin<T, ?>) join;
if ( attributeJoin.isFetched() ) {
if ( join.getSqmJoinType() == SqmJoinType.LEFT ) {
joins.remove( join );
final List<SqmJoin<?, ?>> orderedJoins = findRoot().getOrderedJoins();
if (orderedJoins != null) {
orderedJoins.remove( join );
}
}
else {
attributeJoin.clearFetched();
}
}
}
}
}
}
@Override @Override
public void visitSqmJoins(Consumer<SqmJoin<T, ?>> consumer) { public void visitSqmJoins(Consumer<SqmJoin<T, ?>> consumer) {
if ( joins != null ) { if ( joins != null ) {
@ -386,7 +409,6 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
} }
@Override @Override
@SuppressWarnings("unchecked")
public <K, V> SqmMapJoin<T, K, V> join(MapAttribute<? super T, K, V> attribute, JoinType jt) { public <K, V> SqmMapJoin<T, K, V> join(MapAttribute<? super T, K, V> attribute, JoinType jt) {
final SqmMapJoin<T, K, V> join = buildMapJoin( final SqmMapJoin<T, K, V> join = buildMapJoin(
(MapPersistentAttribute<? super T, K, V>) attribute, (MapPersistentAttribute<? super T, K, V>) attribute,

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.query.sqm.tree.from; package org.hibernate.query.sqm.tree.from;
import org.hibernate.Internal;
import org.hibernate.Remove; import org.hibernate.Remove;
import org.hibernate.metamodel.model.domain.EntityDomainType; import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.criteria.JpaFetch; import org.hibernate.query.criteria.JpaFetch;
@ -37,6 +38,9 @@ public interface SqmAttributeJoin<O,T> extends SqmQualifiedJoin<O,T>, JpaFetch<O
boolean isFetched(); boolean isFetched();
@Internal
void clearFetched();
@Override @Override
SqmPredicate getJoinPredicate(); SqmPredicate getJoinPredicate();
@ -64,5 +68,4 @@ public interface SqmAttributeJoin<O,T> extends SqmQualifiedJoin<O,T>, JpaFetch<O
@Deprecated @Deprecated
@Remove @Remove
SqmAttributeJoin makeCopy( SqmCreationProcessingState creationProcessingState ); SqmAttributeJoin makeCopy( SqmCreationProcessingState creationProcessingState );
} }

View File

@ -36,6 +36,8 @@ import jakarta.persistence.criteria.Selection;
import jakarta.persistence.criteria.Subquery; import jakarta.persistence.criteria.Subquery;
import jakarta.persistence.metamodel.EntityType; import jakarta.persistence.metamodel.EntityType;
import static java.lang.Character.isAlphabetic;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@ -79,11 +81,11 @@ public abstract class AbstractSqmSelectQuery<T>
} }
protected Map<String, SqmCteStatement<?>> copyCteStatements(SqmCopyContext context) { protected Map<String, SqmCteStatement<?>> copyCteStatements(SqmCopyContext context) {
final Map<String, SqmCteStatement<?>> cteStatements = new LinkedHashMap<>( this.cteStatements.size() ); final Map<String, SqmCteStatement<?>> copies = new LinkedHashMap<>( cteStatements.size() );
for ( Map.Entry<String, SqmCteStatement<?>> entry : this.cteStatements.entrySet() ) { for ( Map.Entry<String, SqmCteStatement<?>> entry : cteStatements.entrySet() ) {
cteStatements.put( entry.getKey(), entry.getValue().copy( context ) ); copies.put( entry.getKey(), entry.getValue().copy( context ) );
} }
return cteStatements; return copies;
} }
@Override @Override
@ -154,7 +156,7 @@ public abstract class AbstractSqmSelectQuery<T>
if ( name == null || name.isBlank() ) { if ( name == null || name.isBlank() ) {
throw new IllegalArgumentException( "Illegal empty CTE name" ); throw new IllegalArgumentException( "Illegal empty CTE name" );
} }
if ( !Character.isAlphabetic( name.charAt( 0 ) ) ) { if ( !isAlphabetic( name.charAt( 0 ) ) ) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(
"Illegal CTE name [%s]. Names must start with an alphabetic character!", "Illegal CTE name [%s]. Names must start with an alphabetic character!",

View File

@ -21,6 +21,7 @@ import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Selection; import jakarta.persistence.criteria.Selection;
import org.hibernate.Internal;
import org.hibernate.query.sqm.FetchClauseType; import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.criteria.JpaCriteriaQuery; import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.query.criteria.JpaExpression; import org.hibernate.query.criteria.JpaExpression;
@ -28,16 +29,17 @@ import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SemanticQueryWalker; import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.SqmQuerySource; import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.internal.NoParamSqmCopyContext;
import org.hibernate.query.sqm.internal.SqmUtil; import org.hibernate.query.sqm.internal.SqmUtil;
import org.hibernate.query.sqm.tree.SqmCopyContext; import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmStatement; import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.cte.SqmCteStatement; import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
import org.hibernate.query.sqm.tree.expression.SqmStar;
import org.hibernate.query.sqm.tree.expression.ValueBindJpaCriteriaParameter; import org.hibernate.query.sqm.tree.expression.ValueBindJpaCriteriaParameter;
import org.hibernate.query.sqm.tree.expression.SqmParameter; import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.from.SqmFromClause; import org.hibernate.query.sqm.tree.from.SqmFromClause;
import org.hibernate.query.sqm.tree.from.SqmRoot;
import static org.hibernate.query.sqm.SqmQuerySource.CRITERIA;
import static org.hibernate.query.sqm.tree.SqmCopyContext.noParamCopyContext;
import static org.hibernate.query.sqm.tree.jpa.ParameterCollector.collectParameters; import static org.hibernate.query.sqm.tree.jpa.ParameterCollector.collectParameters;
/** /**
@ -87,7 +89,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
*/ */
public SqmSelectStatement(Class<T> resultJavaType, NodeBuilder nodeBuilder) { public SqmSelectStatement(Class<T> resultJavaType, NodeBuilder nodeBuilder) {
super( resultJavaType, nodeBuilder ); super( resultJavaType, nodeBuilder );
this.querySource = SqmQuerySource.CRITERIA; this.querySource = CRITERIA;
getQuerySpec().setSelectClause( new SqmSelectClause( false, nodeBuilder ) ); getQuerySpec().setSelectClause( new SqmSelectClause( false, nodeBuilder ) );
getQuerySpec().setFromClause( new SqmFromClause() ); getQuerySpec().setFromClause( new SqmFromClause() );
} }
@ -99,7 +101,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
*/ */
public SqmSelectStatement(SqmSelectStatement<T> original) { public SqmSelectStatement(SqmSelectStatement<T> original) {
super( original.getQueryPart(), original.getCteStatementMap(), original.getResultType(), original.nodeBuilder() ); super( original.getQueryPart(), original.getCteStatementMap(), original.getResultType(), original.nodeBuilder() );
this.querySource = SqmQuerySource.CRITERIA; this.querySource = CRITERIA;
} }
private SqmSelectStatement( private SqmSelectStatement(
@ -113,6 +115,54 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
this.parameters = parameters; this.parameters = parameters;
} }
/**
* A query that returns the number of results of this query.
*
* @since 6.5
*/
@Internal
public SqmSelectStatement<Long> countQuery() {
final SqmSelectStatement<?> copy = copy( noParamCopyContext() );
final SqmQuerySpec<?> querySpec = copy.getQuerySpec();
final SqmQueryPart<?> queryPart = copy.getQueryPart();
//TODO: detect queries with no 'group by', but aggregate functions
// in 'select' list (we don't even need to hit the database to
// know they return exactly one row)
if ( queryPart.isSimpleQueryPart()
&& !querySpec.isDistinct()
&& querySpec.getGroupingExpressions().isEmpty() ) {
for ( SqmRoot<?> root : querySpec.getRootList() ) {
root.removeLeftFetchJoins();
}
querySpec.getSelectClause().setSelection( nodeBuilder().count() );
if ( querySpec.getFetch() == null && querySpec.getOffset() == null ) {
querySpec.setOrderByClause( null );
}
@SuppressWarnings("unchecked")
final SqmSelectStatement<Long> statement = (SqmSelectStatement<Long>) copy;
statement.setResultType( Long.class );
return statement;
}
else {
final JpaSelection<?> selection = querySpec.getSelection();
if ( selection.isCompoundSelection() ) {
char c = 'a';
for ( JpaSelection<?> item: selection.getSelectionItems() ) {
item.alias( Character.toString(++c) + '_' );
}
}
else {
selection.alias("a_");
}
final SqmSubQuery<?> subquery = new SqmSubQuery<>( copy, queryPart, null, nodeBuilder() );
final SqmSelectStatement<Long> query = nodeBuilder().createQuery(Long.class);
query.from( subquery );
query.select( nodeBuilder().count() );
return query;
}
}
@Override @Override
public SqmSelectStatement<T> copy(SqmCopyContext context) { public SqmSelectStatement<T> copy(SqmCopyContext context) {
final SqmSelectStatement<T> existing = context.getCopy( this ); final SqmSelectStatement<T> existing = context.getCopy( this );
@ -150,7 +200,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
@Override @Override
public SqmQuerySpec<T> getQuerySpec() { public SqmQuerySpec<T> getQuerySpec() {
if ( querySource == SqmQuerySource.CRITERIA ) { if ( querySource == CRITERIA ) {
final SqmQueryPart<T> queryPart = getQueryPart(); final SqmQueryPart<T> queryPart = getQueryPart();
if ( queryPart instanceof SqmQuerySpec<?> ) { if ( queryPart instanceof SqmQuerySpec<?> ) {
return (SqmQuerySpec<T>) queryPart; return (SqmQuerySpec<T>) queryPart;
@ -210,7 +260,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
@Override @Override
public Set<SqmParameter<?>> getSqmParameters() { public Set<SqmParameter<?>> getSqmParameters() {
if ( querySource == SqmQuerySource.CRITERIA ) { if ( querySource == CRITERIA ) {
assert parameters == null : "SqmSelectStatement (as Criteria) should not have collected parameters"; assert parameters == null : "SqmSelectStatement (as Criteria) should not have collected parameters";
return collectParameters( this ); return collectParameters( this );
} }
@ -253,7 +303,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
// time. // time.
// //
// for a "finalized" set of parameters, use `#resolveParameters` instead // for a "finalized" set of parameters, use `#resolveParameters` instead
assert querySource == SqmQuerySource.CRITERIA; assert querySource == CRITERIA;
return getSqmParameters().stream() return getSqmParameters().stream()
.filter( parameterExpression -> !( parameterExpression instanceof ValueBindJpaCriteriaParameter ) ) .filter( parameterExpression -> !( parameterExpression instanceof ValueBindJpaCriteriaParameter ) )
.collect( Collectors.toSet() ); .collect( Collectors.toSet() );
@ -286,47 +336,47 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
} }
@Override @Override
@SuppressWarnings("unchecked")
public SqmSelectStatement<T> multiselect(List<Selection<?>> selectionList) { public SqmSelectStatement<T> multiselect(List<Selection<?>> selectionList) {
if ( nodeBuilder().isJpaQueryComplianceEnabled() ) { if ( nodeBuilder().isJpaQueryComplianceEnabled() ) {
for ( Selection<?> selection : selectionList ) { for ( Selection<?> selection : selectionList ) {
checkSelectionIsJpaCompliant( selection ); checkSelectionIsJpaCompliant( selection );
} }
} }
final Selection<? extends T> resultSelection = getResultSelection( selectionList );
getQuerySpec().getSelectClause().setSelection( (SqmSelectableNode<?>) resultSelection );
return this;
}
final Selection<? extends T> resultSelection; @SuppressWarnings("unchecked")
private Selection<? extends T> getResultSelection(List<?> selectionList) {
final Class<T> resultType = getResultType(); final Class<T> resultType = getResultType();
final List<? extends JpaSelection<?>> selections = (List<? extends JpaSelection<?>>) (List<?>) selectionList; final List<? extends JpaSelection<?>> selections =
(List<? extends JpaSelection<?>>) selectionList;
if ( resultType == null || resultType == Object.class ) { if ( resultType == null || resultType == Object.class ) {
switch ( selections.size() ) { switch ( selectionList.size() ) {
case 0: { case 0: {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"empty selections passed to criteria query typed as Object" "empty selections passed to criteria query typed as Object"
); );
} }
case 1: { case 1: {
resultSelection = ( Selection<? extends T> ) selections.get( 0 ); return (Selection<? extends T>) selectionList.get( 0 );
break;
} }
default: { default: {
setResultType( (Class<T>) Object[].class ); setResultType( (Class<T>) Object[].class );
resultSelection = ( Selection<? extends T> ) nodeBuilder().array( selections ); return (Selection<? extends T>) nodeBuilder().array( selections );
} }
} }
} }
else if ( Tuple.class.isAssignableFrom( resultType ) ) { else if ( Tuple.class.isAssignableFrom( resultType ) ) {
resultSelection = ( Selection<? extends T> ) nodeBuilder().tuple( selections ); return (Selection<? extends T>) nodeBuilder().tuple( selections );
} }
else if ( resultType.isArray() ) { else if ( resultType.isArray() ) {
resultSelection = nodeBuilder().array( resultType, selections ); return nodeBuilder().array( resultType, selections );
} }
else { else {
resultSelection = nodeBuilder().construct( resultType, selections ); return nodeBuilder().construct( resultType, selections );
} }
getQuerySpec().getSelectClause().setSelection( (SqmSelectableNode<?>) resultSelection );
return this;
} }
private void checkSelectionIsJpaCompliant(Selection<?> selection) { private void checkSelectionIsJpaCompliant(Selection<?> selection) {
@ -460,51 +510,54 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
} }
@Override @Override
public JpaCriteriaQuery<Long> createCountQuery() { public SqmSelectStatement<Long> createCountQuery() {
final SqmCopyContext context = new NoParamSqmCopyContext() { final SqmCopyContext copyContext = noParamCopyContext();
@Override
public boolean copyFetchedFlag() {
return false;
}
};
final NodeBuilder nodeBuilder = nodeBuilder();
final Set<SqmParameter<?>> parameters;
if ( this.parameters == null ) {
parameters = null;
}
else {
parameters = new LinkedHashSet<>( this.parameters.size() );
for ( SqmParameter<?> parameter : this.parameters ) {
parameters.add( parameter.copy( context ) );
}
}
final SqmSelectStatement<Long> selectStatement = new SqmSelectStatement<>(
nodeBuilder,
copyCteStatements( context ),
Long.class,
SqmQuerySource.CRITERIA,
parameters
);
final SqmQuerySpec<Long> querySpec = new SqmQuerySpec<>( nodeBuilder );
final SqmSubQuery<Tuple> subquery = new SqmSubQuery<>( selectStatement, Tuple.class, nodeBuilder ); final SqmQueryPart<T> queryPart = getQueryPart().copy( copyContext );
final SqmQueryPart<T> queryPart = getQueryPart().copy( context );
resetSelections( queryPart ); resetSelections( queryPart );
// Reset the
if ( queryPart.getFetch() == null && queryPart.getOffset() == null ) { if ( queryPart.getFetch() == null && queryPart.getOffset() == null ) {
queryPart.setOrderByClause( null ); queryPart.setOrderByClause( null );
} }
if ( queryPart.isSimpleQueryPart() ) {
for ( SqmRoot<?> root : queryPart.getFirstQuerySpec().getRootList() ) {
root.removeLeftFetchJoins();
}
}
final NodeBuilder nodeBuilder = nodeBuilder();
final Set<SqmParameter<?>> parameters = copyParameters( copyContext );
final SqmSelectStatement<Long> selectStatement = new SqmSelectStatement<>(
nodeBuilder,
copyCteStatements( copyContext ),
Long.class,
CRITERIA,
parameters
);
final SqmSubQuery<Tuple> subquery = new SqmSubQuery<>( selectStatement, Tuple.class, nodeBuilder );
//noinspection unchecked //noinspection unchecked
subquery.setQueryPart( (SqmQueryPart<Tuple>) queryPart ); subquery.setQueryPart( (SqmQueryPart<Tuple>) queryPart );
final SqmQuerySpec<Long> querySpec = new SqmQuerySpec<>( nodeBuilder );
querySpec.setFromClause( new SqmFromClause( 1 ) ); querySpec.setFromClause( new SqmFromClause( 1 ) );
querySpec.setSelectClause( new SqmSelectClause( false, 1, nodeBuilder ) ); querySpec.setSelectClause( new SqmSelectClause( false, 1, nodeBuilder ) );
selectStatement.setQueryPart( querySpec ); selectStatement.setQueryPart( querySpec );
selectStatement.select( nodeBuilder.count( new SqmStar( nodeBuilder ) ) ); selectStatement.select( nodeBuilder.count() );
selectStatement.from( subquery ); selectStatement.from( subquery );
return selectStatement; return selectStatement;
} }
private Set<SqmParameter<?>> copyParameters(SqmCopyContext context) {
if ( parameters == null ) {
return null;
}
else {
final Set<SqmParameter<?>> copied = new LinkedHashSet<>( parameters.size() );
for ( SqmParameter<?> parameter : parameters ) {
copied.add( parameter.copy(context) );
}
return copied;
}
}
private void resetSelections(SqmQueryPart<?> queryPart) { private void resetSelections(SqmQueryPart<?> queryPart) {
if ( queryPart instanceof SqmQuerySpec<?> ) { if ( queryPart instanceof SqmQuerySpec<?> ) {
resetSelections( (SqmQuerySpec<?>) queryPart ); resetSelections( (SqmQuerySpec<?>) queryPart );

View File

@ -615,9 +615,8 @@ public class SqmSubQuery<T> extends AbstractSqmSelectQuery<T> implements SqmSele
} }
private void applyInferableType(Class<T> type) { private void applyInferableType(Class<T> type) {
final EntityDomainType<T> entityDescriptor = nodeBuilder().getSessionFactory().getRuntimeMetamodels() if ( type != null ) {
.getJpaMetamodel() final EntityDomainType<T> entityDescriptor = nodeBuilder().getDomainModel().findEntityType( type );
.findEntityType( type );
if ( entityDescriptor != null ) { if ( entityDescriptor != null ) {
this.expressibleType = entityDescriptor; this.expressibleType = entityDescriptor;
} }
@ -625,6 +624,7 @@ public class SqmSubQuery<T> extends AbstractSqmSelectQuery<T> implements SqmSele
this.expressibleType = nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type ); this.expressibleType = nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType( type );
} }
} }
}
@Override @Override
public SqmExpression<Long> asLong() { public SqmExpression<Long> asLong() {

View File

@ -0,0 +1,47 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.sql.results.spi;
import org.hibernate.Incubating;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.SelectionQuery;
import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl;
import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl;
import org.hibernate.sql.results.jdbc.spi.JdbcValues;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions;
/**
* Used beneath {@link SelectionQuery#getResultCount()}.
*
* @since 6.5
*
* @author Gavin King
*/
@Incubating
public class SingleResultConsumer<T> implements ResultsConsumer<T, T> {
@Override
public T consume(
JdbcValues jdbcValues,
SharedSessionContractImplementor session,
JdbcValuesSourceProcessingOptions processingOptions,
JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState,
RowProcessingStateStandardImpl rowProcessingState,
RowReader<T> rowReader) {
rowReader.getInitializersList().startLoading( rowProcessingState );
rowProcessingState.next();
final T result = rowReader.readRow( rowProcessingState, processingOptions );
rowProcessingState.finishRowProcessing( true );
rowReader.finishUp( jdbcValuesSourceProcessingState );
jdbcValuesSourceProcessingState.finishUp( false );
return result;
}
@Override
public boolean canResultsBeCached() {
return false;
}
}

View File

@ -0,0 +1,152 @@
package org.hibernate.orm.test.query.count;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Root;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SessionFactory
@DomainModel(annotatedClasses = {CountTest.Book.class, CountTest.Author.class, CountTest.Publisher.class})
public class CountTest {
@Test void testCount(SessionFactoryScope scope) {
scope.inTransaction(session -> session.createMutationQuery("delete Book").executeUpdate());
scope.inTransaction(session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
scope.inSession(session -> {
assertEquals(1L,
session.createSelectionQuery("from Book where title like 'Hibernate%'", Book.class)
.getResultCount());
assertEquals(2L,
session.createSelectionQuery("from Book where title like '%Hibernate%'", Book.class)
.getResultCount());
assertEquals(1L,
session.createSelectionQuery("select isbn, title from Book where title like 'Hibernate%'", String.class)
.getResultCount());
assertEquals(1L,
session.createSelectionQuery("from Book where title like :title", Book.class)
.setParameter("title", "Hibernate%")
.getResultCount());
assertEquals(0L,
session.createSelectionQuery("from Book where title like :title", Book.class)
.setParameter("title", "Jibernate%")
.getResultCount());
assertEquals(2L,
session.createSelectionQuery("select title from Book where title like '%Hibernate' union select title from Book where title like 'Hibernate%'", String.class)
.getResultCount());
assertEquals(2L,
session.createSelectionQuery("from Book left join fetch authors left join fetch publisher", Book.class)
.getResultCount());
assertEquals(0L,
session.createSelectionQuery("from Book join fetch publisher", Book.class)
.getResultCount());
});
}
@Test void testCountNative(SessionFactoryScope scope) {
scope.inTransaction(session -> session.createMutationQuery("delete Book").executeUpdate());
scope.inTransaction(session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
scope.inSession(session -> {
assertEquals(2L,
session.createNativeQuery("select title from books", String.class)
.setMaxResults(1)
.getResultCount());
assertEquals(1L,
session.createNativeQuery("select title from books where title like :title", String.class)
.setParameter("title", "Hibernate%")
.getResultCount());
assertEquals(2L,
session.createNativeQuery("select title from books", String.class)
.setMaxResults(1)
.getResultCount());
});
}
@Test void testCountCriteria(SessionFactoryScope scope) {
scope.inTransaction(session -> session.createMutationQuery("delete Book").executeUpdate());
scope.inTransaction(session -> {
session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate"));
});
CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
scope.inSession(session -> {
CriteriaQuery<Book> query1 = builder.createQuery(Book.class);
query1.where( builder.like( query1.from(Book.class).get("title"), "Hibernate%" ) );
assertEquals(1L,
session.createQuery(query1)
.getResultCount());
CriteriaQuery<Book> query2 = builder.createQuery(Book.class);
query2.from(Book.class);
assertEquals(2L,
session.createQuery(query2)
.setMaxResults(1)
.getResultCount());
CriteriaQuery<Book> query3 = builder.createQuery(Book.class);
ParameterExpression<String> parameter = builder.parameter(String.class);
query3.where( builder.like( query3.from(Book.class).get("title"), parameter ) );
assertEquals(1L,
session.createQuery(query3)
.setParameter(parameter, "Hibernate%")
.getResultCount());
CriteriaQuery<Book> query4 = builder.createQuery(Book.class);
Root<Book> book = query4.from(Book.class);
book.fetch("authors", JoinType.INNER);
assertEquals(0L,
session.createQuery(query4)
.getResultCount());
});
}
@Entity(name="Book")
@Table(name = "books")
static class Book {
@Id String isbn;
String title;
@ManyToMany
List<Author> authors;
@ManyToOne
Publisher publisher;
Book(String isbn, String title) {
this.isbn = isbn;
this.title = title;
}
Book() {
}
}
@Entity(name="Author")
@Table(name = "authors")
static class Author {
@Id String ssn;
String name;
}
@Entity(name="Publisher")
@Table(name = "pubs")
static class Publisher {
@Id String name;
}
}

View File

@ -66,7 +66,7 @@ public class CountQueryTests {
"select e from Contact e join fetch e.alternativeContact", "select e from Contact e join fetch e.alternativeContact",
Contact.class Contact.class
) ); ) );
verifyCollectionCount( session, cb.createQuery( verifyCount( session, cb.createQuery(
"select e from Contact e left join fetch e.addresses", "select e from Contact e left join fetch e.addresses",
Contact.class Contact.class
) ); ) );
@ -232,19 +232,6 @@ public class CountQueryTests {
assertEquals( resultList.size(), count.intValue() ); assertEquals( resultList.size(), count.intValue() );
} }
private <T> void verifyCollectionCount(SessionImplementor session, JpaCriteriaQuery<Contact> query) {
final List<Contact> resultList = session.createQuery( query ).getResultList();
final Long count = session.createQuery( query.createCountQuery() ).getSingleResult();
int ormSize = 0;
for ( Contact contact : resultList ) {
ormSize++;
ormSize += Math.max( contact.getAddresses().size() - 1, 0 );
ormSize += Math.max( contact.getPhoneNumbers().size() - 1, 0 );
}
assertEquals( ormSize, count.intValue() );
}
@AfterEach @AfterEach
public void dropTestData(SessionFactoryScope scope) { public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> { scope.inTransaction( (session) -> {