borinquenkid commented on code in PR #15599:
URL: https://github.com/apache/grails-core/pull/15599#discussion_r3142282082
##########
grails-data-hibernate5/docs/src/docs/asciidoc/querying/whereQueries.adoc:
##########
@@ -262,42 +271,60 @@ Operator,Criteria Method,Description
*<=*,sizeLe,The collection size is less than or equal to
|===
-==== Query Aliases and Sorting
-If you define a query for an association an alias is automatically generated
for the query. For example the following query:
+==== Sorting
+
+Sorting with `where` queries is performed by passing `sort` and (optionally)
`order` arguments to the `list` method of a <<detachedCriteria,Detached
Criteria>>.
+
+===== Single-field Sorting
+
+To sort by a single property:
[source,groovy]
----
-def query = Pet.where {
- owner.firstName == "Fred"
+def query = Person.where {
+ firstName == "Jack"
}
+
+def results = query.list(sort: "firstName")
----
-Will generate an alias for the `owner` association such as `owner_alias_0`.
These generated aliases are fine for most cases, but are not useful if you want
to later sort or use a projection on the results. For example the following
query will fail:
+By default, sorting is ascending. You can specify the direction explicitly
using the `order` argument:
Review Comment:
createAlias is supported in HibernateCriteriaBuilder in
grails-datamapping-hibernate7
```java
void "trySimpleCriteria: createAlias delegates to builder.createAlias"() {
when:
invoker.trySimpleCriteria('createAlias',
CriteriaMethods.CREATE_ALIAS, ['transactions', 't'] as Object[])
invoker.trySimpleCriteria('createAlias',
CriteriaMethods.CREATE_ALIAS, ['transactions', 't', 0] as Object[])
then:
1 * builder.createAlias('transactions', 't')
1 * builder.createAlias('transactions', 't', 0)
}
------------
case CREATE_ALIAS:
if (args.length == 2 && args[0] instanceof String s &&
args[1] instanceof String a) {
return builder.createAlias(s, a);
} else if (args.length == 3 &&
args[0] instanceof String s &&
args[1] instanceof String a &&
args[2] instanceof Number jt) {
builder.createAlias(s, a, jt.intValue());
return builder;
}
return name;
------------------
public org.grails.datastore.mapping.query.api.Criteria createAlias(
String associationPath, String alias, int joinType) {
var prop =
hibernateQuery.getEntity().getPropertyByName(associationPath);
JoinType convertedJoinType = convertFromInt(joinType);
if (prop instanceof org.grails.datastore.mapping.model.types.Basic) {
hibernateQuery.addAlias(
new
org.grails.orm.hibernate.query.HibernateAlias(associationPath, alias,
convertedJoinType));
return this;
}
hibernateQuery.getDetachedCriteria().createAlias(associationPath,
alias);
hibernateQuery.getDetachedCriteria().join(associationPath,
convertedJoinType);
return this;
}
```
--
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]