Repository navigation
Keep every alias of an association path in a DetachedCriteria when the datastore opts in (#16851) - #16890
Keep every alias of an association path in a DetachedCriteria when the datastore opts in (#16851)#16890borinquenkid wants to merge 11 commits into
Conversation
…achedCriteria, and reject it clearly on Hibernate 5 (#16851) AbstractDetachedCriteria.createAlias renamed the existing alias of a path when it was given another one, so the first alias was gone before any datastore saw the criteria. A second alias of a path is now kept as its own entry next to the first. Declaring the same alias again, or naming a block that had no alias, behave as before. Hibernate 5 joins an association once, as its Criteria API rejects a second join of one path. It used to reuse the join of the first alias for the second, so a restriction on the second failed with "could not resolve property". It now throws an InvalidDataAccessApiUsageException that names the association and both aliases. Aliases the query generates for associations nobody named are not affected. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
… path (#16851) The AliasMatrixSpec of the 8.0.x line showed that the error turned queries that returned rows on Hibernate 5 into exceptions: a second alias of a path that nothing restricts was harmless before, because the second alias simply reused the join of the first. Only a restriction on the second alias fails, with Hibernate's "could not resolve property". The translator is back to its old behaviour, and the spec pins what fails. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
… association path (#16851) AbstractDetachedCriteria.createAlias replaces the alias of an association path when it is given another one. A MappingContext that implements MultipleAliasesMappingContext and says it joins an association once for each alias now gets every alias as an entry of its own, so the datastore sees both. A datastore that does not implement it, as Mongo, Neo4j, the simple datastore and Hibernate 5 do not, keeps the latest alias of a path, as it always has. The Hibernate 5 spec pins that: the second alias replaces the first, a restriction on it works, and a restriction on the replaced alias fails with Hibernate's own error. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…teria declared earlier (#14647) createAlias on a DetachedCriteria checked every segment of the path against the entities, so after createAlias('children', 'c') the path 'c.related' failed with "Argument [c.related] is not an association". It worked only when the alias had the name of its association, as in createAlias('children', 'children'). A path whose first segment is an alias the criteria declared earlier now continues the path of that alias, so 'c.related' is 'children.related', as the criteria builder resolves it. A first segment that is both an alias and a property of the entity names the alias. The Hibernate 5 spec covers both spellings. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A later join that starts from one of two aliases of the same association is stored as the association path, so two different queries become the same and one returns the wrong rows.
| String head = associationPath.substring(0, dot) | ||
| for (Query.Criterion criterion in criteria) { | ||
| if (criterion instanceof DetachedAssociationCriteria && ((DetachedAssociationCriteria) criterion).alias == head) { | ||
| return ((DetachedAssociationCriteria) criterion).associationPath + associationPath.substring(dot) |
There was a problem hiding this comment.
Input: two aliases of children, then a join from the second alias to related. The stored path is children.related, with nothing that says which alias it starts from. A parent that matches only through the second alias is dropped, or one that matches only through the first is returned. Keep the alias the join started from.
…e alias it started from
createAlias('c2.related', 'r') after two aliases of children was stored as children.related, with nothing
to say that it continues from c2 and not from the first alias, so a datastore that joins each alias
could not tell the two joins apart. The association criteria now carry the written path when it starts
with a later alias of its association. A path that starts with the first alias is the path of the
association and carries nothing.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Fixed in b863104. When a path starts with a later alias of its association, the association criteria now carry the path as written ( |
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A path that starts from a later alias is still stored as the association path, and declaring that alias twice sends the same alias twice.
| // the first alias of an association names the path itself, so a path that starts with it is that path; a | ||
| // later alias is a join of its own, and a path that starts with it has to continue from that join | ||
| String pathFromAlias = startingAlias != null && !startingAlias.is(associationCriteriaMap[startingAlias.associationPath]) ? | ||
| writtenPath : null |
There was a problem hiding this comment.
Create aliases children as c1 and c2, then c2.related as r, then r.owner as o, and compare o.name with Alice. That query is stored as the same criteria as one that starts from c1.related.owner. A parent whose first child relates to Alice and whose second relates to Bob is kept or dropped for both queries. Keep the written alias path when the input starts from a declared alias.
| // is kept next to it, so both aliases reach the datastore, which decides whether they share a join. | ||
| associationCriteria = new DetachedAssociationCriteria(a.associatedEntity.javaClass, a, associationPath, alias) | ||
| associationCriteria.pathFromAlias = pathFromAlias | ||
| add(associationCriteria) |
There was a problem hiding this comment.
Declare children as c1, then declare children as c2 twice. The alias list is c1, c2, c2. The second declaration of c2 is appended again, so the datastore receives the same alias twice. Look up the existing path and alias before adding another criterion.
… join in the criteria builder A second alias for a path that another alias or a block already joins names that join instead of failing with a duplicate association path; a dotted createAlias path joins the associations before its last one; order(Query.Order) accepts a path that starts with an alias; an alias used before its declaration, when a block already joins its path, is rejected with a clear message. The H7 alias path identity matrix now runs on Hibernate 5.
…iteria builder on Hibernate 5 and 7 One path spelled several ways is one join, a dotted createAlias joins the associations before its last one, and an order by an alias-qualified path sorts by that alias through both overloads. Hibernate 5 marks all three pending until the alias fixes of #16890 reach 8.0.x.
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A path that starts with the root alias throws, and a path through an embedded component is turned into a join.
| */ | ||
| protected String joinPrefix(String associationPath, int joinType) { | ||
| String[] segments = associationPath.split("\\."); | ||
| if (segments.length < 2 || aliasMap.containsValue(segments[0])) { |
There was a problem hiding this comment.
createAlias('this.owner', 'o') then eq('o.name', 'A'). The query throws because this is treated as an association. Keep the root alias when the path starts with it.
| for (int i = 0; i < segments.length - 1; i++) { | ||
| absolute.append(i == 0 ? "" : ".").append(segments[i]); | ||
| String existing = aliasMap.get(absolute.toString()); | ||
| if (existing == null) { |
There was a problem hiding this comment.
createAlias('address.country', 'c'), where address is embedded and country is an association inside it. The query throws because address is joined. Keep the embedded segment and join only the association.
… the embedded segment of a path through a component A path that starts with this named a property called 'this' and failed, and a path through an embedded component joined the component, which Hibernate cannot create a criteria on. The path now drops a leading this, and joinPrefix asks the entity metadata, keeping an embedded segment as part of the path and joining only the associations. The two createAlias overloads now share one path, so the rules are written once. AliasPathShapeMatrixSpec runs ten spellings of the path (plain, this, nested, embedded then association, an alias that starts from an earlier alias, a second alias of one path, and the combinations) used to restrict, project and order. 18 of its 30 cells failed before the change. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
matrei
left a comment
There was a problem hiding this comment.
Thanks for picking up #16851 and #14647. I'd like the #14647 fix in 7.0.x, but I don't think this PR can go there as it is. It now contains three separate changes:
- The #14647 fix in
AbstractDetachedCriteria. A path that starts with an alias declared earlier, likecreateAlias('c.related', 'r')aftercreateAlias('children', 'c'), now works. That's a bug fix with tests, and it fits 7.0.x. - The
MultipleAliasesMappingContextopt-in. No datastore on 7.0.x implements it. On 7.0.x it is new public API that never runs: the interface,keepsEveryAlias()andDetachedAssociationCriteria.pathFromAlias. Its only user isHibernateMappingContextin #16891, so I'd move it there (inline comment below). - The Hibernate 5 criteria builder change in 09ad764. The description says Hibernate 5 doesn't change, but this commit changes how
createCriteriaandwithCriteriaresolve aliases. It is the Hibernate 5 side of the #16889 model, not part of #16851 or #14647, so I'd give it a PR of its own.
To see what this means for 7.0.x, I ran the same queries with the Hibernate 5 TCK on the merge base (95f6389) and on 09ad764. Pets Ann (owner A), Bob (owner B) and Cat (no owner). Owners A and B each have one score in round first and one in round second. Owner.address is embedded and has a country association:
| Query | 7.0.x | This PR |
|---|---|---|
Pet.withCriteria { createAlias('this.owner', 'o'); eq('o.name', 'A') } |
[Ann] |
QueryException: could not resolve property: this |
Owner.withCriteria { createAlias('address.country', 'c'); eq('c.name', 'SE') } |
[A] |
QueryException: Criteria objects cannot be created directly on components |
Pet.withCriteria { owner { eq('name', 'A') }; createAlias('owner', 'o'); eq('o.name', 'A') } |
QueryException: duplicate association path: owner |
[Ann] |
Pet.withCriteria { createAlias('owner.scores', 'os'); eq('os.round', 'first'); eq('os.score', 1) } |
SQLGrammarException: column OS1_.ROUND not found |
[Ann] |
Pet.withCriteria { createAlias('owner', 'o'); order(new Query.Order('o.name', DESC)) } |
QueryException: could not resolve property: o |
[Bob, Ann] |
new DetachedCriteria(Owner).build { createAlias('scores', 's'); createAlias('s.judge', 'j'); eq('j.name', 'J1') }.list() |
IllegalArgumentException: Argument [s.judge] is not an association |
[A, B] |
Owner.withCriteria { createAlias('scores', 'a'); createAlias('scores', 'b'); eq('a.round', 'first'); eq('b.round', 'second') } |
QueryException: duplicate association path: scores |
[] |
Domain classes
@Entity
class Judge {
String name
}
@Entity
class Score {
String round
Integer score
Judge judge
}
@Entity
class Country {
String name
}
class Address {
String city
Country country
}
@Entity
class Owner {
String name
Address address
static embedded = ['address']
static hasMany = [scores: Score]
}
@Entity
class Pet {
String name
Owner owner
static constraints = {
owner nullable: true
}
}The first two rows are the cases jamesfredley reported. They work on 7.0.x today and fail with this PR, and we can't ship that in a patch release. The next four rows are real fixes. The #14647 one is in this PR's core change, and I'd be glad to have the other three in 7.0.x once the regressions are fixed and covered by tests.
The last row worries me more, because it doesn't fail. On 7.0.x, two aliases of one path fail with "duplicate association path". With this PR, the second alias names the join of the first, so both restrictions apply to the same score row, and the query returns no owners. In the model in #16896, every later alias has a join of its own, so the same query returns [A, B] on Hibernate 7. Hibernate 5 can't join a path twice, so I'd rather it keeps failing there, as #16851 suggests, and only shares the join of a block that declares no alias (inline comment below).
The PR is also labelled no ci, so its tests haven't run in CI against 7.0.x. #16891 runs on 8.0.x, which is different code, so please replace the label with run ci before this merges.
My proposal:
| if (existing != null && !existing.equals(alias)) { | ||
| if (usedPropertyHeads.contains(alias)) { | ||
| throw new IllegalArgumentException("The alias [" + alias + "] for the association [" + associationPath + | ||
| "] is used before it is declared, and a block of the query already joins that association: " + | ||
| "declare the alias before the criteria that use it"); | ||
| } | ||
| aliasRedirects.put(alias, existing); |
There was a problem hiding this comment.
When existing is an alias that createAlias declared, not one that a block generated, this turns the second alias into another name for the first alias's join. On 7.0.x, createAlias('scores', 'a'), createAlias('scores', 'b'), eq('a.round', 'first'), eq('b.round', 'second') fails with "duplicate association path". With this change it returns no rows, because both restrictions apply to the same join. In the #16896 model, the second alias has a join of its own. Hibernate 5 can't do that, so I'd keep the error when both aliases were declared, and redirect only to the join of a block, for example by recording the aliases the builder generates.
| * it a second alias of a path takes the place of the first, which is what a datastore that ignores aliases, or | ||
| * joins an association once, has always seen. | ||
| * | ||
| * @since 8.0.1 |
There was a problem hiding this comment.
Nothing on 7.0.x implements this. There, this interface, keepsEveryAlias() and pathFromAlias are public API that never runs, and @since 8.0.1 already points at 8.0.x. I'd move them to #16891, together with HibernateMappingContext, their only implementation. That also keeps 7.0.x clear of the open point about a later alias declared twice being added twice, which only matters for a datastore that opts in.
|
@jamesfredley both inputs reproduced and fixed in 74b3759.
Both live in the one path that the two Rather than add two cases, Hibernate 5 core: 737 tests, 0 failures, 29 skipped as before. |
jamesfredley
left a comment
There was a problem hiding this comment.
A path that starts with the root alias no longer throws. Not approving. A path through an embedded component is still turned into a join when the builder has no datastore, or when the path is declared inside an association block. A second alias of an association also fails when it is used to join further.
| if (segments.length < 2 || aliasMap.containsValue(segments[0])) { | ||
| return associationPath; | ||
| } | ||
| PersistentEntity entity = datastore == null ? null : datastore.getMappingContext().getPersistentEntity(targetClass.getName()); |
There was a problem hiding this comment.
createAlias('address.country', 'c') on a builder created with new HibernateCriteriaBuilder(clazz, sessionFactory), or the same path inside owner { }, throws because address is joined. address is embedded. Keep that segment and join only country.
| "] is used before it is declared, and a block of the query already joins that association: " + | ||
| "declare the alias before the criteria that use it"); | ||
| } | ||
| aliasRedirects.put(alias, existing); |
There was a problem hiding this comment.
createAlias('owner', 'o'); createAlias('owner', 'o2'); createAlias('o2.club', 'c') throws. o2 names the same join as o. Use that join when the path starts at o2.
|
Retargeted from 7.0.x to 7.1.x. This adds a public SPI (MultipleAliasesMappingContext), and the PR template keeps 7.0.x (the patch branch) to bug fixes with no new API; 7.1.x is the lowest minor branch and merges up through 7.2.x to 8.0.x. No code changed (head 74b3759). #16891 is the 8.0.x half and still contains this commit. |
…gh a component on a builder without a datastore, and from a second alias
A path through an embedded component was joined when the builder was made without a datastore, because
the component was found only through the mapping context; it is found through the Hibernate metadata then.
createAlias inside an association block did not qualify its path with the alias of the block, so
Hibernate resolved it against the root, and a property that starts at an alias declared in a block was
prefixed with the alias of the block. Both are qualified by the block's alias only when they do not start
at an alias of the query.
A path that starts at a second alias of a join (createAlias('owner', 'o2') after 'o') is the path of the
join the first alias names, so o2.club joins further from it.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley All three cases reproduced, and are fixed in the new commit.
The Hibernate 5 module (740 tests) and Branch policy: the new public type ( If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A second alias inside an association block throws. A path that starts at the root is now looked up on the association.
| String lastAlias = getLastAlias(); | ||
| if (lastAlias != null) { | ||
| // a property that starts at an alias of the query is not a property of the block | ||
| String head = dot > 0 ? propertyName.substring(0, dot) : null; |
There was a problem hiding this comment.
pet { createAlias('owner', 'o'); createAlias('owner', 'ownerAgain'); eq('ownerAgain.name', 'A') } should return the shelf named one. It throws. The dot was counted before the alias was rewritten to a shorter name. Count it again after the rewrite.
|
Thanks for the fixes. I ran the queries from my review again at a21785a. Target branch. I'm afraid 7.1.x doesn't solve the branch question. The examples in the PR template date from before 7.1.0 and 8.0.0 shipped. 7.1.x has had eight patch releases since 7.1.0, so anything merged there ships in a 7.1.x patch release, and in 7.2.x and 8.0.x patch releases after the merge-up. Today 8.1.x (8.1.0-SNAPSHOT) is the only branch whose next release is a minor. So I'd suggest:
Two aliases of one path. This point from my review is still open. Owners
So the same query now fails in one API and silently returns nothing in the other. In the #16896 model, CI. The PR still has |
|
@matrei: the #14647 fix is split out as #17105, on 7.0.x, with no new API: only the path-from-an-earlier-alias change in |
…dation-7.0.x # Conflicts: # grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy
…one keeps the Hibernate error, and the specs register their entities the 8.1.x way Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…lared, not the joins the builder makes for a block Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@matrei: the base is 8.1.x now, as you suggest, and 8.1.x is merged into the branch; against it the pull request changes only its own nine files. The #14647 fix is in #17105 on 7.0.x. This pull request still carries the same lines for it, which go when #17105 is merged up. Two aliases of one path. Hibernate 5 keeps the error, as you said. A second alias that a caller declares for a path that another declared alias already joins is no longer made another name of the first join: Hibernate refuses it with @jamesfredley: |
Part of #16851 and #14647. This is the
7.0.xhalf, so Hibernate 5 and the shared core get it first. The Hibernate 7 half is #16891, which contains this commit and runs the full CI.What changes
AbstractDetachedCriteria.createAliasreplaced the alias of an association path when it was given another one, so the first alias never reached a datastore. AMappingContextthat implements the newMultipleAliasesMappingContextand says it joins an association once for each alias now gets every alias as an entry of its own.A datastore that does not implement it keeps the latest alias of a path, as it always has. That is Hibernate 5, MongoDB, Neo4j and the simple datastore, so none of them changes.
Also in this PR
A
DetachedCriteriaresolves an alias path that starts with an alias it declared earlier: aftercreateAlias('children', 'c')the path'c.related'ischildren.related, as the criteria builder resolves it. It used to fail with "Argument [c.related] is not an association" and worked only when the alias had the name of its association (#14647).Tests
DetachedCriteriaAliasSpecfor Hibernate 5 pins what stays the same: declaring the same alias again is still one alias, the second of two aliases replaces the first and a restriction on it works, and a restriction on the replaced alias fails with Hibernate's own error.The datamapping-core and Hibernate 5 core suites and the style checks pass. The tests of this PR are not run by CI here (
no ci), because the same commit is in #16891.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
Tracked in #16896.