From c6f8e2f741633647846d755e1f0f3561d59ec0fa Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 21 Nov 2023 11:18:55 +0100 Subject: [PATCH] [#1831] Special case mappings with limit of 1 to use = instead of IN predicate --- CHANGELOG.md | 2 +- .../view/impl/metamodel/AbstractAttribute.java | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a9b4d4e76..3fb79b250d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ Changes that happened in releases ### New features -None yet +* Special case mappings with limit of 1 to use `=` instead of `IN` predicate ### Bug fixes diff --git a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/metamodel/AbstractAttribute.java b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/metamodel/AbstractAttribute.java index 467dcf3b81..cd2be2d1b2 100644 --- a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/metamodel/AbstractAttribute.java +++ b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/metamodel/AbstractAttribute.java @@ -176,14 +176,17 @@ public AbstractAttribute(ManagedViewTypeImplementor declaringType, AttributeM batchSize = defaultBatchSize; } + boolean limitOne; String limitExpression; String offsetExpression; List orderByItems; if (mapping.getLimitExpression() == null) { + limitOne = false; limitExpression = null; offsetExpression = null; orderByItems = Collections.emptyList(); } else { + limitOne = "1".equals(mapping.getLimitExpression()); limitExpression = mapping.getLimitExpression(); offsetExpression = mapping.getOffsetExpression(); if (offsetExpression == null || offsetExpression.isEmpty()) { @@ -250,7 +253,7 @@ public AbstractAttribute(ManagedViewTypeImplementor declaringType, AttributeM // This might be due to a @Where annotation being present on the association if (fetchStrategy == FetchStrategy.SELECT && attribute != null && attribute.hasJoinCondition()) { correlated = declaringType.getEntityClass(); - correlationExpression = "this IN __correlationAlias"; + correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias"; correlationResult = mappingString; correlationResultExpression = mappingExpression; } else { @@ -259,7 +262,7 @@ public AbstractAttribute(ManagedViewTypeImplementor declaringType, AttributeM if (attribute == null && (index = mappingString.indexOf('.')) != -1 && mappingString.indexOf('(') == -1 && (attribute = managedType.getOwnedAttributes().get(mappingString.substring(0, index))) != null && !StringUtils.isEmpty(attribute.getMappedBy()) && !attribute.hasJoinCondition()) { correlated = attribute.getElementClass(); - correlationExpression = attribute.getMappedBy() + " IN __correlationAlias"; + correlationExpression = attribute.getMappedBy() + (limitOne ? " = __correlationAlias" : " IN __correlationAlias"); correlationResult = mappingString.substring(index + 1); if (mappingExpression instanceof PathExpression) { correlationResultExpression = ((PathExpression) mappingExpression).withoutFirst(); @@ -268,12 +271,12 @@ public AbstractAttribute(ManagedViewTypeImplementor declaringType, AttributeM } } else if (attribute != null && !StringUtils.isEmpty(attribute.getMappedBy()) && !attribute.hasJoinCondition()) { correlated = attribute.getElementClass(); - correlationExpression = attribute.getMappedBy() + " IN __correlationAlias"; + correlationExpression = attribute.getMappedBy() + (limitOne ? " = __correlationAlias" : " IN __correlationAlias"); correlationResult = ""; correlationResultExpression = new PathExpression(); } else { correlated = declaringType.getEntityClass(); - correlationExpression = "this IN __correlationAlias"; + correlationExpression = limitOne ? "this = __correlationAlias" : "this IN __correlationAlias"; correlationResult = mappingString; correlationResultExpression = mappingExpression; // When using @Limit in combination with JOIN fetching, we need to adapt the correlation expression when array expressions are used