diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java index dc5611500b..2eb456c9d0 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractCriteriaMethod.java @@ -60,15 +60,32 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod { abstract void executeQuery(StringBuilder declaration, List paramTypes); - abstract void createCriteriaQuery(StringBuilder declaration); + abstract String createCriteriaMethod(); abstract String returnType(); + abstract String createQueryMethod(); + @Override void createQuery(StringBuilder declaration) { declaration .append(localSessionName()) - .append(".createQuery(_query)\n"); + .append(".") + .append(createQueryMethod()) + .append("(_query)\n"); + } + + void createCriteriaQuery(StringBuilder declaration) { + final String entityClass = annotationMetaEntity.importType(entity); + declaration + .append("\tvar _query = _builder.") + .append(createCriteriaMethod()) + .append('(') + .append(entityClass) + .append(".class);\n") + .append("\tvar _entity = _query.from(") + .append(entityClass) + .append(".class);\n"); } private void createBuilder(StringBuilder declaration) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java index cd82ae9e5b..c8c2355960 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java @@ -12,6 +12,7 @@ import org.hibernate.processor.model.MetaAttribute; import org.hibernate.processor.model.Metamodel; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; @@ -701,4 +702,18 @@ public abstract class AbstractQueryMethod implements MetaAttribute { } throw new AssertionFailure("could not find parameter"); } + + private static final Set UNSPECIALIZED_QUERY_TYPES + = Set.of(QUERY, TYPED_QUERY, HIB_QUERY); + + static boolean isUnspecializedQueryType(@Nullable String containerType) { + return containerType != null + && UNSPECIALIZED_QUERY_TYPES.contains(containerType); + } + + static boolean isHibernateQueryType(@Nullable String containerType) { + return containerType != null + && containerType.startsWith("org.hibernate"); + } + } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaDeleteMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaDeleteMethod.java index 2136a04595..485a972877 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaDeleteMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaDeleteMethod.java @@ -68,20 +68,20 @@ public class CriteriaDeleteMethod extends AbstractCriteriaMethod { } } + @Override + String createQueryMethod() { + return isUsingEntityManager() || isReactive() + ? "createQuery" + : "createMutationQuery"; + } + private void execute(StringBuilder declaration) { declaration .append("\t\t\t.executeUpdate();\n"); } @Override - void createCriteriaQuery(StringBuilder declaration) { - declaration - .append("\tvar _query = _builder.createCriteriaDelete(") - .append(annotationMetaEntity.importType(entity)) - .append(".class);\n") - .append("\tvar _entity = _query.from(") - .append(annotationMetaEntity.importType(entity)) - .append(".class);\n"); + String createCriteriaMethod() { + return "createCriteriaDelete"; } - } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaFinderMethod.java index e05b7a5be6..dc6fa68fcc 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaFinderMethod.java @@ -85,20 +85,19 @@ public class CriteriaFinderMethod extends AbstractCriteriaMethod { } private void execute(StringBuilder declaration, List paramTypes, boolean unwrapped) { - final boolean mustUnwrap = - containerType != null && containerType.startsWith("org.hibernate"); - executeSelect( declaration, paramTypes, containerType, unwrapped, mustUnwrap ); + executeSelect( declaration, paramTypes, containerType, unwrapped, isHibernateQueryType(containerType) ); } @Override - void createCriteriaQuery(StringBuilder declaration) { - declaration - .append("\tvar _query = _builder.createQuery(") - .append(annotationMetaEntity.importType(entity)) - .append(".class);\n") - .append("\tvar _entity = _query.from(") - .append(annotationMetaEntity.importType(entity)) - .append(".class);\n"); + String createQueryMethod() { + return isUsingEntityManager() || isReactive() || isUnspecializedQueryType(containerType) + ? "createQuery" + : "createSelectionQuery"; + } + + @Override + String createCriteriaMethod() { + return "createQuery"; } @Override diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java index 382c8f7b7d..05fffc402d 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java @@ -14,6 +14,7 @@ import org.hibernate.processor.util.Constants; import java.util.List; import static org.hibernate.processor.util.Constants.LIST; +import static org.hibernate.processor.util.Constants.QUERY; import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase; /** @@ -111,7 +112,8 @@ public class QueryMethod extends AbstractQueryMethod { void createQuery(StringBuilder declaration) { declaration .append(localSessionName()) - .append(isNative ? ".createNativeQuery" : ".createQuery") + .append('.') + .append(createQueryMethod()) .append("(") .append(getConstantName()); if ( returnTypeName != null && !isUpdate ) { @@ -123,6 +125,18 @@ public class QueryMethod extends AbstractQueryMethod { declaration.append(")\n"); } + private String createQueryMethod() { + if ( isNative ) { + return "createNativeQuery"; + } + else if ( isUsingEntityManager() || isReactive() || isUnspecializedQueryType(containerType) ) { + return "createQuery"; + } + else { + return isUpdate ? "createMutationQuery" : "createSelectionQuery"; + } + } + private void castResult(StringBuilder declaration, String returnType) { if ( isNative && returnTypeName != null && containerType == null && isUsingEntityManager() ) { @@ -149,7 +163,7 @@ public class QueryMethod extends AbstractQueryMethod { } else { final boolean mustUnwrap = - containerType != null && containerType.startsWith("org.hibernate") + isHibernateQueryType(containerType) || isNative && returnTypeName != null; executeSelect( declaration, paramTypes, containerType, unwrapped, mustUnwrap ); } @@ -302,6 +316,6 @@ public class QueryMethod extends AbstractQueryMethod { } public String getTypeDeclaration() { - return Constants.QUERY; + return QUERY; } }