Skip to content

Commit 5f50c99

Browse files
committed
HHH-18979 allow Range parameters in query methods
1 parent 25c6060 commit 5f50c99

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hibernate.processor.util.Constants.HIB_ORDER;
2020
import static org.hibernate.processor.util.Constants.HIB_PAGE;
2121
import static org.hibernate.processor.util.Constants.HIB_QUERY;
22+
import static org.hibernate.processor.util.Constants.HIB_RANGE;
2223
import static org.hibernate.processor.util.Constants.HIB_RESTRICTION;
2324
import static org.hibernate.processor.util.Constants.HIB_SELECTION_QUERY;
2425
import static org.hibernate.processor.util.Constants.HIB_SORT_DIRECTION;
@@ -288,8 +289,7 @@ void handlePageParameters(
288289
}
289290

290291
void handleRestrictionParameters(
291-
StringBuilder declaration, List<String> paramTypes,
292-
@Nullable String containerType) {
292+
StringBuilder declaration, List<String> paramTypes) {
293293
for ( int i = 0; i < paramNames.size(); i ++ ) {
294294
final String paramName = paramNames.get(i);
295295
final String paramType = paramTypes.get(i);
@@ -299,6 +299,18 @@ void handleRestrictionParameters(
299299
.append(paramName)
300300
.append(")\n");
301301
}
302+
else if ( isRangeParam(paramType) ) {
303+
declaration
304+
.append("\t\t\t.addRestriction(")
305+
.append(annotationMetaEntity.importType(HIB_RESTRICTION))
306+
.append(".restrict(")
307+
.append(annotationMetaEntity.importType(returnTypeName + '_'))
308+
.append('.')
309+
.append(paramName)
310+
.append(", ")
311+
.append(paramName)
312+
.append("))\n");
313+
}
302314
}
303315
}
304316

@@ -346,6 +358,7 @@ void unwrapQuery(StringBuilder declaration, boolean unwrapped) {
346358
static boolean isSpecialParam(String parameterType) {
347359
return isPageParam(parameterType)
348360
|| isRestrictionParam(parameterType)
361+
|| isRangeParam(parameterType)
349362
|| isOrderParam(parameterType)
350363
|| isKeyedPageParam(parameterType)
351364
|| isSessionParameter(parameterType);
@@ -372,6 +385,10 @@ static boolean isRestrictionParam(String parameterType) {
372385
return parameterType.startsWith(HIB_RESTRICTION);
373386
}
374387

388+
static boolean isRangeParam(String parameterType) {
389+
return parameterType.startsWith(HIB_RANGE);
390+
}
391+
375392
static boolean isJakartaCursoredPage(@Nullable String containerType) {
376393
return JD_CURSORED_PAGE.equals(containerType);
377394
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import static org.hibernate.grammars.hql.HqlLexer.WHERE;
7676
import static org.hibernate.internal.util.StringHelper.qualify;
7777
import static org.hibernate.internal.util.StringHelper.unqualify;
78+
import static org.hibernate.processor.annotation.AbstractQueryMethod.isRangeParam;
7879
import static org.hibernate.processor.annotation.AbstractQueryMethod.isRestrictionParam;
7980
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSessionParameter;
8081
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSpecialParam;
@@ -1819,7 +1820,9 @@ private OrderBy orderByExpression(AnnotationMirror orderBy, TypeElement entityTy
18191820
}
18201821

18211822
private static boolean isFinderParameterMappingToAttribute(VariableElement param) {
1822-
return !isSpecialParam(typeName(param.asType()));
1823+
final String typeName = typeName( param.asType() );
1824+
return !isSpecialParam(typeName)
1825+
|| isRangeParam(typeName);
18231826
}
18241827

18251828
private String[] sessionTypeFromParameters(List<String> paramNames, List<String> paramTypes) {
@@ -2134,14 +2137,18 @@ private boolean checkParameterType(TypeElement entityType, VariableElement param
21342137
parameterType = typeVariable.getUpperBound();
21352138
// INTENTIONAL FALL-THROUGH
21362139
case DECLARED:
2137-
if ( types.isSameType( parameterType, attributeType) ) {
2140+
if ( types.isSameType(parameterType, attributeType) ) {
21382141
return false;
21392142
}
21402143
else {
21412144
final TypeElement list = context.getTypeElementForFullyQualifiedName(LIST);
2142-
if ( types.isSameType( parameterType, types.getDeclaredType( list, attributeType) ) ) {
2145+
final TypeElement range = context.getTypeElementForFullyQualifiedName(HIB_RANGE);
2146+
if ( types.isSameType( parameterType, types.getDeclaredType(list, attributeType) ) ) {
21432147
return true;
21442148
}
2149+
else if ( types.isSameType( parameterType, types.getDeclaredType(range, attributeType) ) ) {
2150+
return false;
2151+
}
21452152
else {
21462153
parameterTypeError( entityType, param, attributeType );
21472154
return false;
@@ -2174,11 +2181,21 @@ private boolean checkParameterType(TypeElement entityType, VariableElement param
21742181

21752182
private void parameterTypeError(TypeElement entityType, VariableElement param, TypeMirror attributeType) {
21762183
message(param,
2177-
"matching field has type '" + attributeType
2184+
"matching field has type '" + stripAnnotations(typeAsString(attributeType))
21782185
+ "' in entity class '" + entityType + "'",
21792186
Diagnostic.Kind.ERROR );
21802187
}
21812188

2189+
private String stripAnnotations(String type) {
2190+
if ( type.startsWith("@") ) {
2191+
final int index = type.lastIndexOf(' ');
2192+
return index > 0 ? type.substring( index + 1 ) : type;
2193+
}
2194+
else {
2195+
return type;
2196+
}
2197+
}
2198+
21822199
private boolean finderParameterNullable(TypeElement entity, VariableElement param) {
21832200
final Element member = memberMatchingPath( entity, parameterName( param ) );
21842201
return isNullable( param )

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaFinderMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void executeQuery(StringBuilder declaration, List<String> paramTypes) {
5858
tryReturn( declaration, paramTypes, containerType );
5959
castResult( declaration );
6060
createQuery( declaration );
61-
handleRestrictionParameters( declaration, paramTypes, containerType );
61+
handleRestrictionParameters( declaration, paramTypes );
6262
handlePageParameters( declaration, paramTypes, containerType );
6363
boolean unwrapped = !isUsingEntityManager();
6464
unwrapped = enableFetchProfile( declaration, unwrapped );

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/QueryMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public String getAttributeDeclarationString() {
9797
tryReturn( declaration, paramTypes, containerType );
9898
castResult( declaration );
9999
createQuery( declaration );
100-
handleRestrictionParameters( declaration, paramTypes, containerType );
100+
handleRestrictionParameters( declaration, paramTypes );
101101
setParameters( declaration, paramTypes, "");
102102
handlePageParameters( declaration, paramTypes, containerType );
103103
boolean unwrapped = !isUsingEntityManager();

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public final class Constants {
9292
public static final String HIB_KEYED_RESULT_LIST = "org.hibernate.query.KeyedResultList";
9393
public static final String HIB_SORT_DIRECTION = "org.hibernate.query.SortDirection";
9494
public static final String HIB_RESTRICTION = "org.hibernate.query.Restriction";
95+
public static final String HIB_RANGE = "org.hibernate.query.range.Range";
9596

9697
public static final String CHECK_HQL = "org.hibernate.annotations.processing.CheckHQL";
9798

0 commit comments

Comments
 (0)