use createMutationQuery() and createSelectionQuery()

squashes a deprecation warning in Maven
This commit is contained in:
Gavin King 2024-03-24 12:25:55 +01:00 committed by Christian Beikov
parent d1734a3964
commit 4dc437b9aa
5 changed files with 70 additions and 25 deletions

View File

@ -60,15 +60,32 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
abstract void executeQuery(StringBuilder declaration, List<String> 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) {

View File

@ -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<String> 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");
}
}

View File

@ -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";
}
}

View File

@ -85,20 +85,19 @@ public class CriteriaFinderMethod extends AbstractCriteriaMethod {
}
private void execute(StringBuilder declaration, List<String> 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

View File

@ -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;
}
}