@@ -96,8 +96,9 @@ public String getAttributeDeclarationString() {
96
96
chainSession ( declaration );
97
97
tryReturn ( declaration , paramTypes , containerType );
98
98
castResult ( declaration );
99
- createQuery ( declaration );
99
+ createSpecification ( declaration );
100
100
handleRestrictionParameters ( declaration , paramTypes );
101
+ createQuery ( declaration );
101
102
setParameters ( declaration , paramTypes , "" );
102
103
handlePageParameters ( declaration , paramTypes , containerType );
103
104
boolean unwrapped = !isUsingEntityManager ();
@@ -109,21 +110,58 @@ public String getAttributeDeclarationString() {
109
110
return declaration .toString ();
110
111
}
111
112
113
+ String specificationType () {
114
+ return isUpdate
115
+ ? "org.hibernate.query.programmatic.MutationSpecification"
116
+ : "org.hibernate.query.programmatic.SelectionSpecification" ;
117
+ }
118
+
112
119
@ Override
113
120
void createQuery (StringBuilder declaration ) {
114
- declaration
115
- .append (localSessionName ())
116
- .append ('.' )
117
- .append (createQueryMethod ())
118
- .append ("(" )
119
- .append (getConstantName ());
120
- if ( returnTypeClass != null && !isUpdate ) {
121
+ if ( returnTypeClass != null && !isReactive () && hasRestriction () ) {
122
+ declaration
123
+ .append ( "\t \t \t .createQuery(" )
124
+ .append ( localSessionName () )
125
+ .append ( ")\n " );
126
+ }
127
+ else {
128
+ // can't use Specification
129
+ declaration
130
+ .append (localSessionName ())
131
+ .append ('.' )
132
+ .append (createQueryMethod ())
133
+ .append ("(" )
134
+ .append (getConstantName ());
135
+ if ( returnTypeClass != null && !isUpdate ) {
136
+ declaration
137
+ .append (", " )
138
+ .append (annotationMetaEntity .importType (returnTypeClass ))
139
+ .append (".class" );
140
+ }
141
+ declaration .append (")\n " );
142
+ }
143
+ }
144
+
145
+ private void createSpecification (StringBuilder declaration ) {
146
+ if ( returnTypeClass != null && !isReactive () && hasRestriction () ) {
121
147
declaration
122
- .append (", " )
123
- .append (annotationMetaEntity .importType (returnTypeClass ))
124
- .append (".class" );
148
+ .append ( annotationMetaEntity .importType ( specificationType () ) )
149
+ .append ( ".create(" )
150
+ .append ( annotationMetaEntity .importType ( returnTypeClass ) )
151
+ .append ( ".class, " )
152
+ .append ( getConstantName () )
153
+ .append ( ")\n " );
125
154
}
126
- declaration .append (")\n " );
155
+ }
156
+
157
+ @ Override
158
+ boolean isUsingSpecification () {
159
+ return returnTypeClass != null && !isReactive () && hasRestriction ();
160
+ }
161
+
162
+ private boolean hasRestriction () {
163
+ return parameterTypes ().stream ()
164
+ .anyMatch ( type -> isRestrictionParam ( type ) || isRangeParam ( type ) );
127
165
}
128
166
129
167
private String createQueryMethod () {
0 commit comments