Skip to content

Commit 572c91c

Browse files
committed
Change annotation processor to use JPA Criteria extension instead of mutating Query
1 parent 0292679 commit 572c91c

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import static org.hibernate.query.sqm.internal.KeyedResult.collectResults;
4646
import static org.hibernate.query.sqm.internal.SqmUtil.isHqlTuple;
4747
import static org.hibernate.query.sqm.internal.SqmUtil.isSelectionAssignableToResultType;
48-
import static org.hibernate.query.sqm.tree.SqmCopyContext.noParamCopyContext;
4948

5049
/**
5150
* @author Gavin King

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.List;
1212
import java.util.StringTokenizer;
1313

14+
import static org.hibernate.processor.util.Constants.HIB_JPA_CRITERIA_QUERY;
15+
import static org.hibernate.processor.util.Constants.HIB_SHARED_SESSION;
1416
import static org.hibernate.processor.util.TypeUtils.getGeneratedClassFullyQualifiedName;
1517
import static org.hibernate.processor.util.TypeUtils.isPrimitive;
1618

@@ -83,6 +85,32 @@ void createQuery(StringBuilder declaration) {
8385
.append("(_query)\n");
8486
}
8587

88+
void createQuery(StringBuilder declaration, List<String> paramTypes, @Nullable String containerType) {
89+
var hasOrdering = hasOrderParameters( containerType, paramTypes );
90+
var hasRestrictions = hasRestrictionParameters( paramTypes );
91+
92+
if ( hasOrdering || hasRestrictions ) {
93+
declaration.append( "((" ).append( annotationMetaEntity.importType(HIB_JPA_CRITERIA_QUERY) ).append( "<" );
94+
declaration.append( annotationMetaEntity.importType(entity) ).append( ">) _query)\n" );
95+
if ( hasOrdering ) {
96+
applyOrder( declaration, paramTypes, containerType, true );
97+
}
98+
if ( hasRestrictions ) {
99+
handleRestrictionParameters( declaration, paramTypes );
100+
}
101+
declaration.append("\t\t\t.toQuery(").append( localSessionName() );
102+
if ( isUsingEntityManager() ) {
103+
declaration.append( ".unwrap(" )
104+
.append( annotationMetaEntity.importType( HIB_SHARED_SESSION ) )
105+
.append( ".class)\n" );
106+
}
107+
declaration.append( ")\n");
108+
}
109+
else {
110+
createQuery( declaration );
111+
}
112+
}
113+
86114
void createCriteriaQuery(StringBuilder declaration) {
87115
final String entityClass = annotationMetaEntity.importType(entity);
88116
declaration

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ else if ( jakartaPageRequest ) {
264264
}
265265
}
266266

267+
boolean hasOrderParameters(@Nullable String containerType, List<String> paramTypes) {
268+
return !isJakartaCursoredPage(containerType) && hasOrdering(paramTypes);
269+
}
270+
267271
boolean applyOrder(
268272
StringBuilder declaration, List<String> paramTypes,
269273
@Nullable String containerType, boolean unwrapped) {
@@ -290,6 +294,19 @@ void handlePageParameters(
290294
}
291295
}
292296

297+
boolean hasRestrictionParameters(List<String> paramTypes) {
298+
for ( int i = 0; i < paramNames.size(); i++ ) {
299+
final String paramType = paramTypes.get( i );
300+
if ( isRestrictionParam( paramType ) ) {
301+
return true;
302+
}
303+
else if ( isRangeParam( paramType ) && returnTypeName != null ) {
304+
return true;
305+
}
306+
}
307+
return false;
308+
}
309+
293310
void handleRestrictionParameters(
294311
StringBuilder declaration, List<String> paramTypes) {
295312
for ( int i = 0; i < paramNames.size(); i ++ ) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ void executeQuery(StringBuilder declaration, List<String> paramTypes) {
5757
collectOrdering( declaration, paramTypes );
5858
tryReturn( declaration, paramTypes, containerType );
5959
castResult( declaration );
60-
createQuery( declaration );
61-
handleRestrictionParameters( declaration, paramTypes );
62-
handlePageParameters( declaration, paramTypes, containerType );
6360
boolean unwrapped = !isUsingEntityManager();
61+
if ( isReactive() ) {
62+
// Reactive doesn't support the createSelectionCriteria() method yet
63+
createQuery( declaration );
64+
handleRestrictionParameters( declaration, paramTypes );
65+
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
66+
}
67+
else {
68+
createQuery( declaration, paramTypes, containerType );
69+
}
70+
handlePageParameters( declaration, paramTypes, containerType );
6471
unwrapped = enableFetchProfile( declaration, unwrapped );
65-
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
6672
execute( declaration, paramTypes, unwrapped );
6773
}
6874

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212

1313
import static org.hibernate.processor.util.Constants.BOOLEAN;
14+
import static org.hibernate.processor.util.Constants.HIB_SHARED_SESSION;
1415
import static org.hibernate.processor.util.Constants.QUERY;
1516
import static org.hibernate.processor.util.Constants.VOID;
1617
import static org.hibernate.processor.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
@@ -96,12 +97,18 @@ public String getAttributeDeclarationString() {
9697
chainSession( declaration );
9798
tryReturn( declaration, paramTypes, containerType );
9899
castResult( declaration );
99-
createQuery( declaration );
100-
handleRestrictionParameters( declaration, paramTypes );
100+
boolean unwrapped = !isUsingEntityManager();
101+
if ( isReactive() ) {
102+
// Reactive doesn't support the createSelectionCriteria() method yet
103+
createQuery( declaration );
104+
handleRestrictionParameters( declaration, paramTypes );
105+
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
106+
}
107+
else {
108+
createQuery( declaration, paramTypes, containerType );
109+
}
101110
setParameters( declaration, paramTypes, "");
102111
handlePageParameters( declaration, paramTypes, containerType );
103-
boolean unwrapped = !isUsingEntityManager();
104-
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
105112
execute( declaration, unwrapped );
106113
convertExceptions( declaration );
107114
chainSessionEnd( isUpdate, declaration );
@@ -126,6 +133,49 @@ void createQuery(StringBuilder declaration) {
126133
declaration.append(")\n");
127134
}
128135

136+
void createQuery(StringBuilder declaration, List<String> paramTypes, @Nullable String containerType) {
137+
var hasOrdering = hasOrderParameters( containerType, paramTypes );
138+
var hasRestrictions = hasRestrictionParameters( paramTypes );
139+
140+
declaration.append(localSessionName());
141+
142+
if ( hasOrdering || hasRestrictions ) {
143+
if ( isUsingEntityManager() ) {
144+
declaration.append( ".unwrap(" )
145+
.append( annotationMetaEntity.importType( HIB_SHARED_SESSION ) )
146+
.append( ".class)\n" );
147+
}
148+
declaration.append("\t\t\t.createSelectionCriteria");
149+
}
150+
else {
151+
declaration.append( '.' ).append( createQueryMethod() );
152+
}
153+
declaration.append("(")
154+
.append(getConstantName());
155+
if ( returnTypeClass != null && !isUpdate ) {
156+
declaration
157+
.append(", ")
158+
.append(annotationMetaEntity.importType(returnTypeClass))
159+
.append(".class");
160+
}
161+
declaration.append(")\n");
162+
if ( hasOrdering || hasRestrictions ) {
163+
if ( hasOrdering ) {
164+
applyOrder( declaration, paramTypes, containerType, true );
165+
}
166+
if ( hasRestrictions ) {
167+
handleRestrictionParameters( declaration, paramTypes );
168+
}
169+
declaration.append("\t\t\t.toQuery(").append( localSessionName() );
170+
if ( isUsingEntityManager() ) {
171+
declaration.append( ".unwrap(" )
172+
.append( annotationMetaEntity.importType( HIB_SHARED_SESSION ) )
173+
.append( ".class)\n" );
174+
}
175+
declaration.append( ")\n");
176+
}
177+
}
178+
129179
private String createQueryMethod() {
130180
if ( isNative ) {
131181
return "createNativeQuery";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public final class Constants {
103103
public static final String HIB_QUERY = "org.hibernate.query.Query";
104104
public static final String HIB_SELECTION_QUERY = "org.hibernate.query.SelectionQuery";
105105
public static final String HIB_SESSION = "org.hibernate.Session";
106+
public static final String HIB_SHARED_SESSION = "org.hibernate.SharedSessionContract";
107+
public static final String HIB_JPA_CRITERIA_QUERY = "org.hibernate.query.criteria.JpaCriteriaQuery";
106108
public static final String HIB_SESSION_FACTORY = "org.hibernate.SessionFactory";
107109
public static final String HIB_STATELESS_SESSION = "org.hibernate.StatelessSession";
108110
public static final String MUTINY_SESSION_FACTORY = "org.hibernate.reactive.mutiny.Mutiny.SessionFactory";

0 commit comments

Comments
 (0)