HHH-15106 - fk() SQM function

This commit is contained in:
Steve Ebersole 2022-03-08 21:00:08 -06:00
parent 415b28f116
commit 1fe1110f0b
4 changed files with 54 additions and 0 deletions

View File

@ -103,9 +103,12 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
<T> JpaCriteriaQuery<T> except(boolean all, CriteriaQuery<? extends T> query1, CriteriaQuery<?>... queries);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Paths
<P, F> JpaExpression<F> fk(Path<P> path);
@Override
<X, T extends X> JpaPath<T> treat(Path<X> path, Class<T> type);

View File

@ -23,6 +23,7 @@ import org.hibernate.query.criteria.JpaCoalesce;
import org.hibernate.query.criteria.JpaCompoundSelection;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.criteria.JpaParameterExpression;
import org.hibernate.query.criteria.JpaPath;
import org.hibernate.query.criteria.JpaSearchedCase;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.criteria.JpaSimpleCase;
@ -146,6 +147,9 @@ public interface NodeBuilder extends HibernateCriteriaBuilder {
@Override
SqmPredicate wrap(Expression<Boolean>... expressions);
@Override
<P, F> SqmExpression<F> fk(Path<P> path);
@Override
<X, T extends X> SqmPath<T> treat(Path<X> path, Class<T> type);

View File

@ -39,6 +39,7 @@ import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.BindableType;
import org.hibernate.query.SemanticException;
import org.hibernate.query.sqm.BinaryArithmeticOperator;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.NullPrecedence;
@ -64,6 +65,8 @@ import org.hibernate.query.sqm.spi.SqmCreationContext;
import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
import org.hibernate.query.sqm.tree.domain.SqmBagJoin;
import org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath;
import org.hibernate.query.sqm.tree.domain.SqmFkExpression;
import org.hibernate.query.sqm.tree.domain.SqmListJoin;
import org.hibernate.query.sqm.tree.domain.SqmMapJoin;
import org.hibernate.query.sqm.tree.domain.SqmPath;
@ -132,6 +135,7 @@ import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Selection;
import jakarta.persistence.criteria.SetJoin;
import jakarta.persistence.criteria.Subquery;
import jakarta.persistence.metamodel.Bindable;
import static java.util.Arrays.asList;
import static org.hibernate.query.internal.QueryHelper.highestPrecedenceType;
@ -342,6 +346,19 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
return new SqmJunctionPredicate( Predicate.BooleanOperator.AND, predicates, this );
}
@Override
public <P, F> SqmExpression<F> fk(Path<P> path) {
if ( path.getModel().getBindableType() != Bindable.BindableType.SINGULAR_ATTRIBUTE ) {
throw new SemanticException( "Path should refer to a to-one attribute : " + path );
}
if ( ! ( path instanceof SqmEntityValuedSimplePath ) ) {
throw new SemanticException( "Path should refer to a to-one attribute : " + path );
}
return new SqmFkExpression<>( (SqmEntityValuedSimplePath) path, this );
}
@Override
public <X, T extends X> SqmPath<T> treat(Path<X> path, Class<T> type) {
return ( (SqmPath<X>) path ).treatAs( type );

View File

@ -0,0 +1,30 @@
/*
* 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.orm.test.query.criteria;
import org.hibernate.testing.orm.domain.StandardDomainModel;
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;
/**
* Simple {@link org.hibernate.query.sqm.NodeBuilder} tests
*
* @author Steve Ebersole
*/
@DomainModel( standardModels = StandardDomainModel.RETAIL )
@SessionFactory
public class NodeBuilderTests {
@Test
public void testFkExpression(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final String hql = "from Order o where fk(o.salesAssociate) = 1";
session.createSelectionQuery( hql ).getResultList();
} );
}
}