HHH-17772 support Jakarta Data PageRequest as a parameter
This commit is contained in:
parent
be448afdab
commit
df84bcd84e
|
@ -11,7 +11,6 @@ import org.hibernate.jpamodelgen.model.MetaAttribute;
|
|||
import org.hibernate.jpamodelgen.model.Metamodel;
|
||||
import org.hibernate.jpamodelgen.util.Constants;
|
||||
import org.hibernate.query.Order;
|
||||
import org.hibernate.query.Page;
|
||||
import org.hibernate.query.SortDirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -20,9 +19,13 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HIB_ORDER;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HIB_PAGE;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_LIMIT;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_ORDER;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE_REQUEST;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.LIST;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
|
||||
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
|
||||
|
||||
|
@ -183,22 +186,30 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
|
||||
void setPage(StringBuilder declaration, String paramName, String paramType) {
|
||||
boolean jakartaLimit = JD_LIMIT.equals(paramType);
|
||||
if ( jakartaLimit || isUsingEntityManager() ) {
|
||||
declaration
|
||||
.append("\n\t\t\t.setFirstResult(");
|
||||
if (jakartaLimit) {
|
||||
declaration.append("(int) ");
|
||||
boolean jakartaPageRequest = paramType.startsWith(JD_PAGE_REQUEST);
|
||||
if ( jakartaLimit || jakartaPageRequest
|
||||
|| isUsingEntityManager() ) {
|
||||
final String firstResult;
|
||||
final String maxResults;
|
||||
if ( jakartaLimit ) {
|
||||
firstResult = "(int) " + paramName + ".startAt() - 1";
|
||||
maxResults = paramName + ".maxResults()";
|
||||
}
|
||||
else if ( jakartaPageRequest ) {
|
||||
firstResult = "(int) (" + paramName + ".page()-1) * " + paramName + ".size()";
|
||||
maxResults = paramName + ".size()";
|
||||
}
|
||||
else {
|
||||
firstResult = paramName + ".getFirstResult()";
|
||||
maxResults = paramName + ".getMaxResults()";
|
||||
}
|
||||
declaration
|
||||
.append(paramName)
|
||||
.append('.')
|
||||
.append(jakartaLimit ? "startAt" : "getFirstResult")
|
||||
.append("())")
|
||||
.append("\n\t\t\t.setFirstResult(")
|
||||
.append(firstResult)
|
||||
.append(")")
|
||||
.append("\n\t\t\t.setMaxResults(")
|
||||
.append(paramName)
|
||||
.append('.')
|
||||
.append(jakartaLimit ? "maxResults" : "getMaxResults")
|
||||
.append("())");
|
||||
.append(maxResults)
|
||||
.append(")");
|
||||
}
|
||||
else {
|
||||
declaration
|
||||
|
@ -341,13 +352,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
}
|
||||
|
||||
static boolean isPageParam(String parameterType) {
|
||||
return Page.class.getName().equals(parameterType)
|
||||
|| JD_LIMIT.equals(parameterType);
|
||||
return HIB_PAGE.equals(parameterType)
|
||||
|| JD_LIMIT.equals(parameterType)
|
||||
|| parameterType.startsWith(JD_PAGE_REQUEST);
|
||||
}
|
||||
|
||||
static boolean isOrderParam(String parameterType) {
|
||||
return parameterType.startsWith(Order.class.getName())
|
||||
|| parameterType.startsWith(List.class.getName() + "<" + Order.class.getName())
|
||||
return parameterType.startsWith(HIB_ORDER)
|
||||
|| parameterType.startsWith(LIST + "<" + HIB_ORDER)
|
||||
|| parameterType.startsWith(JD_SORT)
|
||||
|| parameterType.startsWith(JD_ORDER);
|
||||
}
|
||||
|
|
|
@ -68,6 +68,7 @@ import static org.hibernate.internal.util.StringHelper.qualify;
|
|||
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isOrderParam;
|
||||
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isPageParam;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.FIND;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HIB_ORDER;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HIB_SESSION;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HIB_STATELESS_SESSION;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.HQL;
|
||||
|
@ -76,11 +77,13 @@ import static org.hibernate.jpamodelgen.util.Constants.JD_DELETE;
|
|||
import static org.hibernate.jpamodelgen.util.Constants.JD_FIND;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_INSERT;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_ORDER;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE_REQUEST;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_QUERY;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_REPOSITORY;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_SAVE;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_UPDATE;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.LIST;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.MUTINY_SESSION;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
|
||||
import static org.hibernate.jpamodelgen.util.Constants.SQL;
|
||||
|
@ -820,16 +823,17 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
validateFinderParameter( entity, parameter );
|
||||
}
|
||||
else {
|
||||
final Types types = context.getTypeUtils();
|
||||
final TypeMirror parameterType = parameter.asType();
|
||||
if ( isOrderParam( parameterType.toString() ) ) {
|
||||
final String type = parameterType.toString();
|
||||
boolean pageRequest = type.startsWith(JD_PAGE_REQUEST);
|
||||
if ( isOrderParam(type) || pageRequest ) {
|
||||
final TypeMirror typeArgument = getTypeArgument( parameterType );
|
||||
if ( typeArgument == null ) {
|
||||
context.message( parameter, "missing type of order (should be 'Order<? super "
|
||||
+ entity.getSimpleName() + ">')", Diagnostic.Kind.ERROR );
|
||||
missingTypeArgError( entity.getSimpleName().toString(), parameter, pageRequest );
|
||||
}
|
||||
else if ( !context.getTypeUtils().isSameType( typeArgument, entity.asType() ) ) {
|
||||
context.message( parameter, "mismatched type of order (should be 'Order<? super "
|
||||
+ entity.getSimpleName() + ">')", Diagnostic.Kind.ERROR);
|
||||
else if ( !types.isSameType( typeArgument, entity.asType() ) ) {
|
||||
wrongTypeArgError( entity.getSimpleName().toString(), parameter, pageRequest );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -854,6 +858,24 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
);
|
||||
}
|
||||
|
||||
private void wrongTypeArgError(String entity, VariableElement parameter, boolean pageRequest) {
|
||||
context.message(parameter,
|
||||
(pageRequest
|
||||
? "mismatched type of page request (should be 'PageRequest<? super "
|
||||
:"mismatched type of order (should be 'Order<? super ")
|
||||
+ entity + ">')",
|
||||
Diagnostic.Kind.ERROR );
|
||||
}
|
||||
|
||||
private void missingTypeArgError(String entity, VariableElement parameter, boolean pageRequest) {
|
||||
context.message(parameter,
|
||||
(pageRequest
|
||||
? "missing type of page request (should be 'PageRequest<? super "
|
||||
: "missing type of order (should be 'Order<? super ")
|
||||
+ entity + ">')",
|
||||
Diagnostic.Kind.ERROR );
|
||||
}
|
||||
|
||||
private List<OrderBy> orderByList(ExecutableElement method, TypeElement entityType) {
|
||||
final AnnotationMirror orderByList =
|
||||
getAnnotationMirror( method, "jakarta.data.repository.OrderBy.List" );
|
||||
|
@ -898,14 +920,15 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
case DECLARED:
|
||||
final DeclaredType type = (DeclaredType) parameterType;
|
||||
final String parameterTypeName = parameterType.toString();
|
||||
if ( parameterTypeName.startsWith( List.class.getName() ) ) {
|
||||
if ( parameterTypeName.startsWith( LIST ) ) {
|
||||
for (TypeMirror arg : type.getTypeArguments()) {
|
||||
return getTypeArgument( arg );
|
||||
}
|
||||
}
|
||||
else if ( parameterTypeName.startsWith( Order.class.getName() )
|
||||
else if ( parameterTypeName.startsWith( HIB_ORDER )
|
||||
|| parameterTypeName.startsWith( JD_SORT )
|
||||
|| parameterTypeName.startsWith( JD_ORDER )) {
|
||||
|| parameterTypeName.startsWith( JD_ORDER )
|
||||
|| parameterTypeName.startsWith( JD_PAGE_REQUEST ) ) {
|
||||
for ( TypeMirror arg : type.getTypeArguments() ) {
|
||||
switch ( arg.getKind() ) {
|
||||
case WILDCARD:
|
||||
|
@ -1756,17 +1779,18 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
}
|
||||
}
|
||||
if ( returnType != null ) {
|
||||
final Types types = context.getTypeUtils();
|
||||
for ( VariableElement parameter : method.getParameters() ) {
|
||||
final TypeMirror parameterType = parameter.asType();
|
||||
final TypeMirror typeArgument = getTypeArgument( parameterType );
|
||||
if ( isOrderParam( parameterType.toString() ) ) {
|
||||
final String type = parameterType.toString();
|
||||
final boolean pageRequest = type.startsWith(JD_PAGE_REQUEST);
|
||||
if ( isOrderParam( type ) || pageRequest) {
|
||||
if ( typeArgument == null ) {
|
||||
context.message( parameter, "missing type of order (should be 'Order<? super "
|
||||
+ returnType + ">')", Diagnostic.Kind.ERROR );
|
||||
missingTypeArgError( returnType.toString(), parameter, pageRequest );
|
||||
}
|
||||
else if ( !context.getTypeUtils().isSameType(typeArgument, returnType) ) {
|
||||
context.message( parameter, "mismatched type of order (should be 'Order<? super "
|
||||
+ returnType + ">')", Diagnostic.Kind.ERROR );
|
||||
else if ( !types.isSameType(typeArgument, returnType) ) {
|
||||
wrongTypeArgError( returnType.toString(), parameter, pageRequest );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,9 +69,13 @@ public final class Constants {
|
|||
public static final String JD_DELETE = "jakarta.data.repository.Delete";
|
||||
public static final String JD_SAVE = "jakarta.data.repository.Save";
|
||||
public static final String JD_LIMIT = "jakarta.data.Limit";
|
||||
public static final String JD_PAGE_REQUEST = "jakarta.data.page.PageRequest";
|
||||
public static final String JD_SORT = "jakarta.data.Sort";
|
||||
public static final String JD_ORDER = "jakarta.data.Order";
|
||||
|
||||
public static final String HIB_ORDER = "org.hibernate.query.Order";
|
||||
public static final String HIB_PAGE = "org.hibernate.query.Page";
|
||||
|
||||
public static final String CHECK_HQL = "org.hibernate.annotations.processing.CheckHQL";
|
||||
|
||||
public static final String ENTITY_MANAGER = "jakarta.persistence.EntityManager";
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.hibernate.jpamodelgen.test.data;
|
|||
import jakarta.data.Limit;
|
||||
import jakarta.data.Order;
|
||||
import jakarta.data.Sort;
|
||||
import jakarta.data.page.PageRequest;
|
||||
import jakarta.data.repository.By;
|
||||
import jakarta.data.repository.Delete;
|
||||
import jakarta.data.repository.Find;
|
||||
|
@ -94,4 +95,10 @@ public interface BookAuthorRepository {
|
|||
|
||||
@Query("select title from Book where title like :title order by isbn")
|
||||
Stream<String> titles(String title);
|
||||
|
||||
@Query("from Book")
|
||||
List<Book> everyBook1(PageRequest<? super Book> pageRequest);
|
||||
|
||||
@Find
|
||||
List<Book> everyBook2(PageRequest<? super Book> pageRequest);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue