@@ -176,14 +176,17 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
176
176
batchSize = defaultBatchSize ;
177
177
}
178
178
179
+ boolean limitOne ;
179
180
String limitExpression ;
180
181
String offsetExpression ;
181
182
List <OrderByItem > orderByItems ;
182
183
if (mapping .getLimitExpression () == null ) {
184
+ limitOne = false ;
183
185
limitExpression = null ;
184
186
offsetExpression = null ;
185
187
orderByItems = Collections .emptyList ();
186
188
} else {
189
+ limitOne = "1" .equals (mapping .getLimitExpression ());
187
190
limitExpression = mapping .getLimitExpression ();
188
191
offsetExpression = mapping .getOffsetExpression ();
189
192
if (offsetExpression == null || offsetExpression .isEmpty ()) {
@@ -250,7 +253,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
250
253
// This might be due to a @Where annotation being present on the association
251
254
if (fetchStrategy == FetchStrategy .SELECT && attribute != null && attribute .hasJoinCondition ()) {
252
255
correlated = declaringType .getEntityClass ();
253
- correlationExpression = "this IN __correlationAlias" ;
256
+ correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias" ;
254
257
correlationResult = mappingString ;
255
258
correlationResultExpression = mappingExpression ;
256
259
} else {
@@ -259,7 +262,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
259
262
if (attribute == null && (index = mappingString .indexOf ('.' )) != -1 && mappingString .indexOf ('(' ) == -1
260
263
&& (attribute = managedType .getOwnedAttributes ().get (mappingString .substring (0 , index ))) != null && !StringUtils .isEmpty (attribute .getMappedBy ()) && !attribute .hasJoinCondition ()) {
261
264
correlated = attribute .getElementClass ();
262
- correlationExpression = attribute .getMappedBy () + " IN __correlationAlias" ;
265
+ correlationExpression = attribute .getMappedBy () + ( limitOne ? " = __correlationAlias" : " IN __correlationAlias") ;
263
266
correlationResult = mappingString .substring (index + 1 );
264
267
if (mappingExpression instanceof PathExpression ) {
265
268
correlationResultExpression = ((PathExpression ) mappingExpression ).withoutFirst ();
@@ -268,12 +271,12 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
268
271
}
269
272
} else if (attribute != null && !StringUtils .isEmpty (attribute .getMappedBy ()) && !attribute .hasJoinCondition ()) {
270
273
correlated = attribute .getElementClass ();
271
- correlationExpression = attribute .getMappedBy () + " IN __correlationAlias" ;
274
+ correlationExpression = attribute .getMappedBy () + ( limitOne ? " = __correlationAlias" : " IN __correlationAlias") ;
272
275
correlationResult = "" ;
273
276
correlationResultExpression = new PathExpression ();
274
277
} else {
275
278
correlated = declaringType .getEntityClass ();
276
- correlationExpression = "this IN __correlationAlias" ;
279
+ correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias" ;
277
280
correlationResult = mappingString ;
278
281
correlationResultExpression = mappingExpression ;
279
282
// When using @Limit in combination with JOIN fetching, we need to adapt the correlation expression when array expressions are used
0 commit comments