Skip to content

Commit c6f8e2f

Browse files
committed
[#1831] Special case mappings with limit of 1 to use = instead of IN predicate
1 parent a05387e commit c6f8e2f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Changes that happened in releases
66

77
### New features
88

9-
None yet
9+
* Special case mappings with limit of 1 to use `=` instead of `IN` predicate
1010

1111
### Bug fixes
1212

entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/metamodel/AbstractAttribute.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
176176
batchSize = defaultBatchSize;
177177
}
178178

179+
boolean limitOne;
179180
String limitExpression;
180181
String offsetExpression;
181182
List<OrderByItem> orderByItems;
182183
if (mapping.getLimitExpression() == null) {
184+
limitOne = false;
183185
limitExpression = null;
184186
offsetExpression = null;
185187
orderByItems = Collections.emptyList();
186188
} else {
189+
limitOne = "1".equals(mapping.getLimitExpression());
187190
limitExpression = mapping.getLimitExpression();
188191
offsetExpression = mapping.getOffsetExpression();
189192
if (offsetExpression == null || offsetExpression.isEmpty()) {
@@ -250,7 +253,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
250253
// This might be due to a @Where annotation being present on the association
251254
if (fetchStrategy == FetchStrategy.SELECT && attribute != null && attribute.hasJoinCondition()) {
252255
correlated = declaringType.getEntityClass();
253-
correlationExpression = "this IN __correlationAlias";
256+
correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias";
254257
correlationResult = mappingString;
255258
correlationResultExpression = mappingExpression;
256259
} else {
@@ -259,7 +262,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
259262
if (attribute == null && (index = mappingString.indexOf('.')) != -1 && mappingString.indexOf('(') == -1
260263
&& (attribute = managedType.getOwnedAttributes().get(mappingString.substring(0, index))) != null && !StringUtils.isEmpty(attribute.getMappedBy()) && !attribute.hasJoinCondition()) {
261264
correlated = attribute.getElementClass();
262-
correlationExpression = attribute.getMappedBy() + " IN __correlationAlias";
265+
correlationExpression = attribute.getMappedBy() + (limitOne ? " = __correlationAlias" : " IN __correlationAlias");
263266
correlationResult = mappingString.substring(index + 1);
264267
if (mappingExpression instanceof PathExpression) {
265268
correlationResultExpression = ((PathExpression) mappingExpression).withoutFirst();
@@ -268,12 +271,12 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
268271
}
269272
} else if (attribute != null && !StringUtils.isEmpty(attribute.getMappedBy()) && !attribute.hasJoinCondition()) {
270273
correlated = attribute.getElementClass();
271-
correlationExpression = attribute.getMappedBy() + " IN __correlationAlias";
274+
correlationExpression = attribute.getMappedBy() + (limitOne ? " = __correlationAlias" : " IN __correlationAlias");
272275
correlationResult = "";
273276
correlationResultExpression = new PathExpression();
274277
} else {
275278
correlated = declaringType.getEntityClass();
276-
correlationExpression = "this IN __correlationAlias";
279+
correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias";
277280
correlationResult = mappingString;
278281
correlationResultExpression = mappingExpression;
279282
// When using @Limit in combination with JOIN fetching, we need to adapt the correlation expression when array expressions are used

0 commit comments

Comments
 (0)