Repository navigation
fix(hibernate5): an association block that is a branch of or keeps the entities without the association #16931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.0.x
Are you sure you want to change the base?
Changes from all commits
e4ca252
bba38ae
9ab5c9b
b4565f1
d7651a9
4b09d87
f3241f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,10 @@ | |
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import groovy.lang.Closure; | ||
| import groovy.lang.DelegatesTo; | ||
|
|
@@ -137,6 +139,8 @@ public abstract class AbstractHibernateCriteriaBuilder extends GroovyObjectSuppo | |
| protected List<String> aliasStack = new ArrayList<>(); | ||
| protected List<Criteria> aliasInstanceStack = new ArrayList<>(); | ||
| protected Map<String, String> aliasMap = new HashMap<>(); | ||
| /** The aliases of association blocks that are branches of an "or", which join the association with a left join. */ | ||
| protected Set<String> leftBlockAliases = new HashSet<>(); | ||
| protected static final String ALIAS = "_alias"; | ||
| protected ResultTransformer resultTransformer; | ||
| protected int aliasCount; | ||
|
|
@@ -1863,7 +1867,7 @@ else if (paginationEnabledList) { | |
| // Property type is a managed entity - treat as association | ||
| Class oldTargetClass = targetClass; | ||
| targetClass = propertyType; | ||
| if (targetClass.equals(oldTargetClass) && !hasMoreThanOneArg) { | ||
| if ((targetClass.equals(oldTargetClass) || isInDisjunction()) && !hasMoreThanOneArg) { | ||
| joinType = org.hibernate.sql.JoinType.LEFT_OUTER_JOIN.getJoinTypeValue(); | ||
| } | ||
| associationStack.add(name); | ||
|
|
@@ -1892,12 +1896,28 @@ else if (paginationEnabledList) { | |
| if (attribute.isAssociation()) { | ||
| Class oldTargetClass = targetClass; | ||
| targetClass = getClassForAssociationType(attribute); | ||
| if (targetClass.equals(oldTargetClass) && !hasMoreThanOneArg) { | ||
| joinType = org.hibernate.sql.JoinType.LEFT_OUTER_JOIN.getJoinTypeValue(); // default to left join if joining on the same table | ||
| final boolean branchOfOr = isInDisjunction() && !hasMoreThanOneArg; | ||
| if ((targetClass.equals(oldTargetClass) || branchOfOr) && !hasMoreThanOneArg) { | ||
| // default to left join if joining on the same table, or when the block is a branch of an | ||
| // "or": the entities without the association are still found by the other branches | ||
| joinType = org.hibernate.sql.JoinType.LEFT_OUTER_JOIN.getJoinTypeValue(); | ||
| } | ||
| associationStack.add(name); | ||
| final String associationPath = getAssociationPath(); | ||
| final boolean aliasExisted = aliasMap.containsKey(associationPath); | ||
| createAliasIfNeccessary(name, associationPath, joinType); | ||
| final String blockAlias = getLastAlias(); | ||
| if (branchOfOr && !aliasExisted) { | ||
| leftBlockAliases.add(blockAlias); | ||
| } | ||
| // a block that is a branch of an "or" asks for an associated entity, and so does a block that | ||
| // reuses the left join of one, as a block outside an "or" always did; a block that names an inner | ||
| // join asks for it as well, as the join it reuses is not the inner join the caller asked for. | ||
| // (A left join the caller named is kept as the caller named it.) | ||
| final boolean explicitInnerJoin = hasMoreThanOneArg && | ||
| joinType == org.hibernate.sql.JoinType.INNER_JOIN.getJoinTypeValue(); | ||
| final boolean requiresAssociated = branchOfOr || | ||
| (leftBlockAliases.contains(blockAlias) && (!hasMoreThanOneArg || explicitInnerJoin)); | ||
| // the criteria within an association node are grouped with an implicit AND | ||
| logicalExpressionStack.add(new LogicalExpression(AND)); | ||
| invokeClosureNode(callable); | ||
|
|
@@ -1906,6 +1926,15 @@ else if (paginationEnabledList) { | |
| aliasInstanceStack.remove(aliasInstanceStack.size() - 1); | ||
| } | ||
| LogicalExpression logicalExpression = logicalExpressionStack.remove(logicalExpressionStack.size() - 1); | ||
| if (logicalExpression.args.isEmpty() && requiresAssociated) { | ||
| // a block without criteria is not a branch of the "or": it asks for an associated entity of | ||
| // the entity as a whole, as the inner join it was always given did | ||
| criteria.add(Restrictions.isNotNull(blockAlias + ".id")); | ||
| } | ||
| else if (requiresAssociated) { | ||
| // a missing associated entity is a null that a null test in the block would match | ||
| logicalExpression.args.add(0, Restrictions.isNotNull(blockAlias + ".id")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with has a child named kid. orphan has no child. An or that asks for the child, then a second or with child(0), should return only with. It returns with and orphan. The existence check sits inside the second or, so the name branch bypasses it. Keep that check on the whole query. |
||
| } | ||
| if (!logicalExpression.args.isEmpty()) { | ||
| addToCriteria(logicalExpression.toCriterion()); | ||
| } | ||
|
|
@@ -1984,6 +2013,15 @@ private boolean isAssociationQueryWithJoinSpecificationMethod(Object[] args) { | |
| return args.length == 2 && (args[0] instanceof Number) && (args[1] instanceof Closure); | ||
| } | ||
|
|
||
| private boolean isInDisjunction() { | ||
| for (LogicalExpression expression : logicalExpressionStack) { | ||
| if (OR.equals(expression.name)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private void createAliasIfNeccessary(String associationName, String associationPath, int joinType) { | ||
| String newAlias; | ||
| if (aliasMap.containsKey(associationPath)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.gorm.tests | ||
|
|
||
| import org.hibernate.criterion.CriteriaSpecification | ||
| import spock.lang.AutoCleanup | ||
| import spock.lang.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| import grails.gorm.annotation.Entity | ||
| import grails.gorm.transactions.Rollback | ||
| import org.grails.orm.hibernate.HibernateDatastore | ||
|
|
||
| /** | ||
| * An association block that names its join type keeps it: a block that asks for a left join finds the entities without | ||
| * the association, whether it is a branch of an {@code or}, or follows a block in one that joined it with a left join | ||
| * because it is a branch of the {@code or}. | ||
| */ | ||
| @Rollback | ||
| class CriteriaOrAssociationJoinTypeSpec extends Specification { | ||
|
|
||
| @Shared | ||
| @AutoCleanup | ||
| HibernateDatastore datastore = new HibernateDatastore(JoinParent, JoinChild) | ||
|
|
||
| private void createParents() { | ||
| new JoinParent(name: 'with', child: new JoinChild(name: 'kid')).save(flush: true) | ||
| new JoinParent(name: 'other', child: new JoinChild(name: 'stranger')).save(flush: true) | ||
| new JoinParent(name: 'orphan').save(flush: true) | ||
| } | ||
|
|
||
| void 'a block that asks for a left join and is a branch of or keeps the entities without the association'() { | ||
| given: | ||
| createParents() | ||
|
|
||
| when: | ||
| def found = JoinParent.createCriteria().list { | ||
| or { | ||
| child(CriteriaSpecification.LEFT_JOIN) { isNull('name') } | ||
| eq('name', 'nobody') | ||
| } | ||
| } | ||
|
|
||
| then: | ||
| found*.name == ['orphan'] | ||
| } | ||
|
|
||
| void 'a later block that asks for a left join keeps the entities without the association'() { | ||
| given: | ||
| createParents() | ||
|
|
||
| when: | ||
| def found = JoinParent.createCriteria().list { | ||
| or { | ||
| child { eq('name', 'kid') } | ||
| eq('name', 'orphan') | ||
| } | ||
| child(CriteriaSpecification.LEFT_JOIN) { isNull('name') } | ||
| } | ||
|
|
||
| then: | ||
| found*.name == ['orphan'] | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class JoinParent { | ||
|
|
||
| String name | ||
| JoinChild child | ||
|
|
||
| static constraints = { | ||
| child(nullable: true) | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class JoinChild { | ||
|
|
||
| String name | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.