HHH-17724 add count(*) to HibernateCriteriaBuilder

and remove some unused type parameters
This commit is contained in:
Gavin King 2024-02-08 12:15:23 +01:00
parent ecd0acb735
commit b571b22777
5 changed files with 41 additions and 13 deletions

View File

@ -389,6 +389,11 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
@Override
JpaExpression<Long> countDistinct(Expression<?> x);
/**
* Equivalent to HQL {@code count(*)}.
*/
JpaExpression<Long> count();
@Override
<N extends Number> JpaExpression<N> neg(Expression<N> x);
@ -2680,7 +2685,7 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
* @since 6.4
*/
@Incubating
<T> JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, Expression<String> separatorExpression);
JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, Expression<String> separatorExpression);
/**
* Concatenates the non-null array elements with a separator, as specified by the arguments.
@ -2688,7 +2693,7 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
* @since 6.4
*/
@Incubating
<T> JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator);
JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator);
/**
* Whether an array contains an element.

View File

@ -439,6 +439,11 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.count( argument );
}
@Override
public JpaExpression<Long> count() {
return criteriaBuilder.count();
}
@Override
public JpaExpression<Long> countDistinct(Expression<?> x) {
return criteriaBuilder.countDistinct( x );
@ -2435,7 +2440,7 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
@Override
@Incubating
public <T> JpaExpression<String> arrayToString(
public JpaExpression<String> arrayToString(
Expression<? extends Object[]> arrayExpression,
Expression<String> separatorExpression) {
return criteriaBuilder.arrayToString( arrayExpression, separatorExpression );
@ -2443,7 +2448,7 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
@Override
@Incubating
public <T> JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator) {
public JpaExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator) {
return criteriaBuilder.arrayToString( arrayExpression, separator );
}

View File

@ -17,7 +17,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Incubating;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.query.NullPrecedence;
@ -32,7 +31,6 @@ import org.hibernate.query.criteria.JpaPredicate;
import org.hibernate.query.criteria.JpaSearchedCase;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.criteria.JpaSimpleCase;
import org.hibernate.query.criteria.JpaValues;
import org.hibernate.query.criteria.JpaWindow;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
@ -240,10 +238,10 @@ public interface NodeBuilder extends HibernateCriteriaBuilder {
<T> SqmExpression<T[]> arrayFill(T element, Integer elementCount);
@Override
<T> SqmExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, Expression<String> separatorExpression);
SqmExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, Expression<String> separatorExpression);
@Override
<T> SqmExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator);
SqmExpression<String> arrayToString(Expression<? extends Object[]> arrayExpression, String separator);
@Override
<T> SqmPredicate arrayContains(Expression<T[]> arrayExpression, Expression<T> elementExpression);
@ -596,6 +594,9 @@ public interface NodeBuilder extends HibernateCriteriaBuilder {
@Override
SqmExpression<Long> countDistinct(Expression<?> x);
@Override
SqmExpression<Long> count();
@Override
<N extends Number> SqmExpression<N> neg(Expression<N> x);

View File

@ -44,7 +44,6 @@ import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.model.domain.BasicDomainType;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.metamodel.model.domain.internal.BasicTypeImpl;
import org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor;
@ -77,7 +76,6 @@ import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SetOperator;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.TrimSpec;
import org.hibernate.query.sqm.UnaryArithmeticOperator;
@ -121,6 +119,7 @@ import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.query.sqm.tree.expression.SqmLiteralNull;
import org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression;
import org.hibernate.query.sqm.tree.expression.SqmOver;
import org.hibernate.query.sqm.tree.expression.SqmStar;
import org.hibernate.query.sqm.tree.expression.SqmToDuration;
import org.hibernate.query.sqm.tree.expression.SqmTrimSpecification;
import org.hibernate.query.sqm.tree.expression.SqmTuple;
@ -182,7 +181,6 @@ import jakarta.persistence.metamodel.Bindable;
import static java.util.Arrays.asList;
import static org.hibernate.query.internal.QueryHelper.highestPrecedenceType;
import static org.hibernate.query.sqm.TrimSpec.fromCriteriaTrimSpec;
import static org.hibernate.query.sqm.internal.TypecheckUtil.assertComparable;
/**
* Acts as a JPA {@link jakarta.persistence.criteria.CriteriaBuilder} by
@ -855,6 +853,15 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
);
}
@Override
public SqmExpression<Long> count() {
return getFunctionDescriptor( "count" ).generateSqmExpression(
new SqmStar( this ),
null,
queryEngine
);
}
@Override
public JpaExpression<Integer> sign(Expression<? extends Number> x) {
return getFunctionDescriptor( "sign" ).generateSqmExpression(
@ -4280,7 +4287,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
}
@Override
public <T> SqmExpression<String> arrayToString(
public SqmExpression<String> arrayToString(
Expression<? extends Object[]> arrayExpression,
Expression<String> separatorExpression) {
return getFunctionDescriptor( "array_to_string" ).generateSqmExpression(
@ -4291,7 +4298,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
}
@Override
public <T> SqmExpression<String> arrayToString(
public SqmExpression<String> arrayToString(
Expression<? extends Object[]> arrayExpression,
String separator) {
return getFunctionDescriptor( "array_to_string" ).generateSqmExpression(

View File

@ -10,6 +10,7 @@ import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
@ -34,5 +35,14 @@ public class CountEntityWithCompositeIdTest {
assertThat(entityManager.createQuery(cq).getSingleResult().intValue(), is(0));
}
);
scope.inTransaction(
entityManager -> {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
cq.from(EntityWithCompositeId.class);
cq.select(cb.count());
assertThat(entityManager.createQuery(cq).getSingleResult().intValue(), is(0));
}
);
}
}