Skip to content

Commit

Permalink
[#1831] Special case mappings with limit of 1 to use = instead of IN …
Browse files Browse the repository at this point in the history
…predicate
  • Loading branch information
beikov committed Nov 23, 2023
1 parent a05387e commit c6f8e2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,17 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
batchSize = defaultBatchSize;
}

boolean limitOne;
String limitExpression;
String offsetExpression;
List<OrderByItem> 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()) {
Expand Down Expand Up @@ -250,7 +253,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> 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 {
Expand All @@ -259,7 +262,7 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> 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();
Expand All @@ -268,12 +271,12 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> 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
Expand Down

0 comments on commit c6f8e2f

Please sign in to comment.