ruthst00 commented on code in PR #16573:
URL: https://github.com/apache/grails-core/pull/16573#discussion_r4415131059
##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/PredicateGenerator.java:
##########
@@ -306,20 +305,11 @@ private Predicate handleAssociationCriteria(
JpaQueryContext fromsByProvider,
DetachedAssociationCriteria<?> associationCriteria) {
String associationName = associationCriteria.getAssociationPath();
- From<?, ?> associationRoot = fromsByProvider.getFrom(associationName);
- if (associationRoot == null) {
- // Check if we already have it in our parent or alias map
- Expression<?> expr =
fromsByProvider.getFullyQualifiedExpression(associationName);
- if (expr instanceof From<?, ?> from) {
- associationRoot = from;
- } else {
- associationRoot =
fromsByProvider.getRoot().join(associationName);
- fromsByProvider.addFrom(associationName, associationRoot);
- }
- }
+ From<?, ?> associationRoot =
fromsByProvider.resolveAssociation(associationName);
// Create a nested context for this association
- JpaQueryContext nestedContext = new JpaQueryContext(fromsByProvider,
null, associationRoot);
+ JpaQueryContext nestedContext = new
QueryAliasRegistrar(associationCriteria, null)
+ .createContext(fromsByProvider, associationRoot);
Review Comment:
Fixed in
https://github.com/apache/grails-core/pull/16573/changes/ece3fd3d52a7b914a0cbf598d476e8b877312ac4
##########
grails-doc/src/en/ref/Domain Classes/createCriteria.adoc:
##########
@@ -75,10 +75,42 @@ The `sort` and `order` arguments accepted alongside the
pagination values are ch
=== Description
+Criteria queries are a type-safe, advanced way to query that uses a Groovy
builder to construct potentially complex queries. It is a much better
alternative to using a StringBuilder to dynamically construct an HQL query.
Refer to the criteria sections in the GORM for
link:{hibernate7GuideFromRef}index.html#criteria[Hibernate 7] or
link:{hibernate5GuideFromRef}index.html#criteria[Hibernate 5] guide for usage
instructions.
-Criteria queries are a type-safe, advanced way to query that uses a Groovy
builder to construct potentially complex queries. It is a much better
alternative to using a StringBuilder to dynamically construct an HQL query.
Refer to the user guide section on
link:{hibernate5GuideFromRef}index.html#criteria[Criteria] for usage
instructions.
+==== Association aliases
-Method reference:
+With Hibernate, `createAlias('association', 'alias')` creates an inner join
and lets you refer to the association as `alias.property`.
+
+In both Hibernate 5 and Hibernate 7, the three-argument overload accepts `0`
for an inner join, `1` for a left outer join, or `2` for a right outer join.
Hibernate 5 also supports `4` for a full join. Hibernate 7 does not support
full joins through this overload: any value other than `1` or `2`, including
`4`, produces an inner join.
+
+For example, to include books with no author as well as books whose author has
a name:
+
+[source,groovy]
+----
+def books = Book.createCriteria().list {
+ createAlias('author', 'a', 1) // left outer join
+ or {
+ isNull('author')
+ isNotNull('a.name')
+ }
+}
+----
+
+The alias uses the requested join type. A left join alone does not guarantee
that unmatched rows remain in the result: the restrictions must also allow the
missing association, as the `or` block does above.
+
+In Hibernate 7, every explicitly requested alias of an entity association
creates a join even when no restriction, projection, or order references it.
Declaring a left alias and an inner alias for the same association therefore
excludes rows with no associated entity, even if the inner alias is otherwise
unused.
+
+An alias declared inside an association block is relative to that association,
not the outer domain class. In Hibernate 7, aliases declared in a `list { ...
}` closure do not carry over to a subsequent `list { ... }` call on the same
criteria builder.
Review Comment:
Fixed in
https://github.com/apache/grails-core/pull/16573/changes/ece3fd3d52a7b914a0cbf598d476e8b877312ac4
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]