diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/type/dynamicparameterized/DynamicParameterizedTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/type/dynamicparameterized/DynamicParameterizedTypeTest.java index c177628689..15629d5d69 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/type/dynamicparameterized/DynamicParameterizedTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/type/dynamicparameterized/DynamicParameterizedTypeTest.java @@ -23,22 +23,44 @@ */ package org.hibernate.orm.test.annotations.type.dynamicparameterized; -import java.util.Date; - +import org.hibernate.boot.model.FunctionContributions; +import org.hibernate.boot.model.FunctionContributor; +import org.hibernate.metamodel.mapping.BasicValuedMapping; +import org.hibernate.metamodel.model.domain.BasicDomainType; +import org.hibernate.metamodel.model.domain.internal.BasicSqmPathSource; +import org.hibernate.query.ReturnableType; +import org.hibernate.query.sqm.produce.function.FunctionReturnTypeResolver; +import org.hibernate.query.sqm.tree.SqmTypedNode; +import org.hibernate.sql.ast.tree.SqlAstNode; +import org.hibernate.testing.orm.junit.BootstrapServiceRegistry; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.type.CustomType; +import org.hibernate.type.spi.TypeConfiguration; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.Date; +import java.util.List; +import java.util.function.Supplier; + +import static org.junit.jupiter.api.Assertions.*; /** * @author Daniel Gredler + * @author Jan-Willem Gmelig Meyling + * @author Sayra Ranjha */ @DomainModel( annotatedClasses = { AbstractEntity.class, Entity1.class, Entity2.class } ) @SessionFactory +@BootstrapServiceRegistry( + javaServices = @BootstrapServiceRegistry.JavaService( + role = FunctionContributor.class, + impl = DynamicParameterizedTypeTest.FunctionContributorImpl.class + ) +) public class DynamicParameterizedTypeTest { @Test @@ -75,4 +97,36 @@ public class DynamicParameterizedTypeTest { } ); } + + @Test + public void testMetamodel(SessionFactoryScope scope) { + scope.inTransaction(session -> { + List resultList = session.createQuery("select test_func1(a.entity1_Prop3) from Entity1 a").getResultList(); + assertNotNull(resultList); + }); + } + + + public static class FunctionContributorImpl implements FunctionContributor { + @Override + public void contributeFunctions(FunctionContributions functionContributions) { + functionContributions.getFunctionRegistry() + .patternDescriptorBuilder( "test_func1", "?1" ) + .setReturnTypeResolver(new FunctionReturnTypeResolver() { + @Override + public ReturnableType resolveFunctionReturnType(ReturnableType impliedType, List> arguments, TypeConfiguration typeConfiguration) { + SqmTypedNode sqmTypedNode = arguments.get(0); + BasicDomainType sqmPathType = ((BasicSqmPathSource) sqmTypedNode.getNodeType()).getSqmPathType(); + assertInstanceOf(CustomType.class, sqmPathType); + return null; + } + + @Override + public BasicValuedMapping resolveFunctionReturnType(Supplier impliedTypeAccess, List arguments) { + return null; + } + }) + .register(); + } + } }