Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0676c67
Sort where queries by an association the query filters on
matrei Nov 4, 2026
b494caa
Document that a sort reaches one association away
matrei Nov 4, 2026
2990df1
Revert "Document that a sort reaches one association away"
jdaugherty Nov 4, 2026
c0c5a80
Sort where queries by a nested association path
jdaugherty Nov 4, 2026
5e71443
Sort by a nested association id through its foreign key
jdaugherty Nov 4, 2026
2154153
Join an association for a sort with a left join
jdaugherty Nov 4, 2026
1b70845
Join an association with a left join for every sort
jdaugherty Nov 4, 2026
ea2c5e8
Sort a paged criteria query by a nested association path
jdaugherty Nov 4, 2026
c6b4d61
Sort list by every entry of a sort map
jdaugherty Nov 4, 2026
b482d67
Join an association for a sort when the query is executed
jdaugherty Nov 5, 2026
4646683
Apply a requested join type without criteria or a sort
jdaugherty Nov 5, 2026
e6343c4
Apply a join type that a criteria query requests
jdaugherty Nov 5, 2026
4dabb45
Apply the join types of a subquery after its criteria
jdaugherty Nov 5, 2026
226bbc8
Fetch a collection that a right join is requested for
jdaugherty Nov 5, 2026
d689aab
Recognize a join that criteria make through an alias
jdaugherty Nov 5, 2026
97dbd76
Let a later fetch mode replace a requested join type
jdaugherty Nov 5, 2026
a34a260
Cover the default sort and data services in the upgrade notes
jdaugherty Nov 5, 2026
c5b0edb
Join a collection without fetching it unless a fetch is requested
jdaugherty Nov 5, 2026
f4ef5b1
Join a requested path through a collection in a subquery
jdaugherty Nov 5, 2026
71b4535
Make the joins of a query when the query is executed
jdaugherty Nov 5, 2026
6ce701e
Apply the session filters to a join requested for a collection
jdaugherty Nov 5, 2026
b9f26d4
Let a later fetch mode replace a join type in every query
jdaugherty Nov 5, 2026
8ef8631
Join an association once for a sort by several of its properties
jdaugherty Nov 5, 2026
9c59ac4
Sort a criteria query by an association path with order
jdaugherty Nov 5, 2026
4988d60
Add the orders of a criteria query once its criteria are built
jdaugherty Nov 6, 2026
97bd0e5
Fetch a collection that a query sorted by it asks to fetch
jdaugherty Nov 6, 2026
5a94a73
Order a criteria query by an association id without a join
jdaugherty Nov 6, 2026
7e847ed
Resolve a Query.Order in the criteria of an association against it
jdaugherty Nov 6, 2026
75b5b4d
Do not fetch a collection through the left join of its criteria
jdaugherty Nov 6, 2026
60450f9
Use a short alias for the subquery of a requested join
jdaugherty Nov 6, 2026
e8f7036
Correct two statements about a requested join type
jdaugherty Nov 6, 2026
cc58a3b
Apply a requested join type to updateAll and deleteAll
jdaugherty Nov 6, 2026
1ab83ab
Reject a bulk join that MySQL cannot run with a clear error
jdaugherty Nov 6, 2026
688440e
Fix the regressions in requested join types and association sorts
jdaugherty Nov 6, 2026
0c81f00
Fix older join type and sort issues and correct the docs
jdaugherty Nov 6, 2026
5e6dc8f
Reject a sort by a bag in a query that fetches another bag
jdaugherty Nov 8, 2026
1313011
Fetch the associations of a requested join type and never filter a co…
jdaugherty Nov 8, 2026
b4e50b4
Reject a sort by an alias that the query does not define
borinquenkid Nov 8, 2026
1e47513
fix(hibernate5): the fetch mode join of list() fetches with a left jo…
borinquenkid Nov 8, 2026
9f2c781
Keep the root alias on the count of a paged list
jdaugherty Nov 9, 2026
8df78b0
Count each instance once for a list that fetches a collection
jdaugherty Nov 9, 2026
0c312b0
Reject a sort by a bag in list when it fetches another bag
jdaugherty Nov 9, 2026
f7ee8a3
Join a filtered collection with an inner join when another is fetched
jdaugherty Nov 9, 2026
deb2784
Cover an undefined sort alias and the list fetch mode in the upgrade …
jdaugherty Nov 9, 2026
8c4df83
Reject a sort by a collection next to a fetch when either one is a bag
jdaugherty Nov 9, 2026
01ede72
Give the subquery of a requested join an alias apart from the root
jdaugherty Nov 9, 2026
a6346ce
Keep a sort join under a fetch in the count of a paged list
jdaugherty Nov 9, 2026
bdd9a85
Reject a fetched bag at any depth next to a sort by a collection
jdaugherty Nov 9, 2026
a5bfa75
Keep a requested left join and load other fetched collections later
jdaugherty Nov 9, 2026
0d97b4c
Apply the bag check to what a query actually loads
jdaugherty Nov 9, 2026
056588e
Follow the values of a map when loading collections after a query
jdaugherty Nov 9, 2026
3677b3a
Treat a fetch from the mapping as a fetch of the sorted collection
jdaugherty Nov 10, 2026
7323d92
Sort by a path that starts with the alias of the queried class
jdaugherty Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public HibernateCriteriaBuilder(Class targetClass, SessionFactory sessionFactory
* @see #createAlias(String, String)
*/
public Criteria createAlias(String associationPath, String alias, int joinType) {
aliasMap.put(associationPath, alias);
aliasMap.put(getWholeAssociationPath(associationPath), alias);
return criteria.createAlias(associationPath, alias, JoinType.parse(joinType));
}

Expand Down Expand Up @@ -233,8 +233,9 @@ public static org.hibernate.criterion.DetachedCriteria getHibernateDetachedCrite
else {
detachedCriteria = org.hibernate.criterion.DetachedCriteria.forClass(targetClass);
}
populateHibernateDetachedCriteria(new HibernateQuery(detachedCriteria, persistentEntity), detachedCriteria, queryableCriteria);
return detachedCriteria;
HibernateQuery subquery = new HibernateQuery(detachedCriteria, persistentEntity);
populateHibernateDetachedCriteria(subquery, detachedCriteria, queryableCriteria);
return subquery.getHibernateDetachedCriteria();
}

private static void populateHibernateDetachedCriteria(AbstractHibernateQuery query, org.hibernate.criterion.DetachedCriteria detachedCriteria, QueryableCriteria<?> queryableCriteria) {
Expand All @@ -243,6 +244,11 @@ private static void populateHibernateDetachedCriteria(AbstractHibernateQuery que
Map<String, FetchType> fetchStrategies = abstractDetachedCriteria.getFetchStrategies();
for (Entry<String, FetchType> entry : fetchStrategies.entrySet()) {
String property = entry.getKey();
jakarta.persistence.criteria.JoinType supersededJoinType = abstractDetachedCriteria.getSupersededJoinTypes().get(property);
if (supersededJoinType != null) {
// requested before the fetch mode below, which replaces it
query.join(property, supersededJoinType);
}
switch (entry.getValue()) {
case EAGER:
jakarta.persistence.criteria.JoinType gormJoinType = abstractDetachedCriteria.getJoinTypes().get(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.hibernate.FlushMode
import org.hibernate.LockMode
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.jpa.QueryHints
import org.hibernate.query.Query

import org.springframework.core.convert.ConversionService
Expand Down Expand Up @@ -91,6 +92,7 @@ class HibernateGormStaticApi<D> extends AbstractHibernateGormStaticApi<D> {
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(persistentEntity.javaClass)
Root queryRoot = criteriaQuery.from(persistentEntity.javaClass)
GrailsHibernateQueryUtils.populateArgumentsForCriteria(
sessionFactory,
persistentEntity,
criteriaQuery,
queryRoot,
Expand All @@ -100,6 +102,11 @@ class HibernateGormStaticApi<D> extends AbstractHibernateGormStaticApi<D> {
true
)
Query query = session.createQuery(criteriaQuery)
if (criteriaQuery.isDistinct()) {
// a query that fetches a collection returns each instance once, without a distinct in the SQL,
// which would compare every fetched column and reject an order by a column that it does not select
query.setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, false)
}

GrailsHibernateQueryUtils.populateArgumentsForCriteria(
persistentEntity,
Expand Down Expand Up @@ -237,7 +244,10 @@ class HibernateGormStaticApi<D> extends AbstractHibernateGormStaticApi<D> {

@Override
protected void firePreQueryEvent(Session session, Criteria criteria) {
datastore.applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, new HibernateQuery(criteria, persistentEntity)))
HibernateQuery query = new HibernateQuery(criteria, persistentEntity)
datastore.applicationEventPublisher.publishEvent(new PreQueryEvent(datastore, query))
// makes the joins that criteria a listener added need, since the query itself is not executed
query.getHibernateCriteria()
}

@Override
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ public HibernateQuery(DetachedCriteria criteria, PersistentEntity entity) {
* @return The hibernate criteria
*/
public Criteria getHibernateCriteria() {
applyDeferredJoins();
return this.criteria;
}

/**
* @return The hibernate detached criteria of a query that is used as a subquery. The joins of
* the subquery are made here, once its criteria have been added and the join types that it
* requests are known.
*/
public DetachedCriteria getHibernateDetachedCriteria() {
applyDeferredJoins();
return this.detachedCriteria;
}

@Override
protected AbstractHibernateCriterionAdapter createHibernateCriterionAdapter() {
return HIBERNATE_CRITERION_ADAPTER;
Expand Down Expand Up @@ -108,11 +119,12 @@ protected Dialect getDialect(SessionFactory sessionFactory) {

@Override
public Object clone() {
applyDeferredJoins();
Comment thread
jdaugherty marked this conversation as resolved.
Resolved
final CriteriaImpl impl = (CriteriaImpl) criteria;
final HibernateSession hibernateSession = (HibernateSession) getSession();
final GrailsHibernateTemplate hibernateTemplate = (GrailsHibernateTemplate) hibernateSession.getNativeInterface();
return hibernateTemplate.execute((GrailsHibernateTemplate.HibernateCallback<Object>) session -> {
Criteria newCriteria = session.createCriteria(impl.getEntityOrClassName());
Criteria newCriteria = session.createCriteria(impl.getEntityOrClassName(), impl.getAlias());

Iterator iterator = impl.iterateExpressionEntries();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Root;

import org.hibernate.HibernateException;
Expand Down Expand Up @@ -63,7 +67,17 @@ public int getTotalCount() {
if (totalCount == Integer.MIN_VALUE) {
totalCount = hibernateTemplate.execute(new GrailsHibernateTemplate.HibernateCallback<>() {
public Integer doInHibernate(Session session) throws HibernateException, SQLException {
final CriteriaQuery finalQuery = criteriaQuery.select(criteriaBuilder.count(queryRoot)).distinct(true).orderBy();
// a count does not select the associations that the list fetched, and Hibernate rejects
// a fetch of an association whose owner the query does not select. A sort through a fetched
// association joins from the fetch, and the list pages through the rows of that join
if (!queryRoot.getFetches().isEmpty()) {
joinWhatIsJoinedFromFetches(queryRoot, queryRoot, "count");
queryRoot.getFetches().clear();
}
// a list that fetches a collection returns each instance once, whatever a sort joins; any other
// list returns a row for each element of a collection that it sorts by, and pages through those rows
final Expression count = criteriaQuery.isDistinct() ? criteriaBuilder.countDistinct(queryRoot) : criteriaBuilder.count(queryRoot);
final CriteriaQuery finalQuery = criteriaQuery.select(count).orderBy();
final Query query = session.createQuery(finalQuery);
hibernateTemplate.applySettings(query);
return ((Number) query.uniqueResult()).intValue();
Expand All @@ -73,6 +87,49 @@ public Integer doInHibernate(Session session) throws HibernateException, SQLExce
return totalCount;
}

/**
* Joins, from the given target, the fetches of the given parent that have joins of their own, and
* those joins. Each join gets an alias from its path, since Hibernate would generate one that the
* root of the query already has from rendering the list.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
private static void joinWhatIsJoinedFromFetches(From target, From parent, String aliasPrefix) {
for (Object existing : parent.getFetches()) {
if (existing instanceof From && hasJoins((From) existing)) {
From fetch = (From) existing;
String name = ((Fetch) fetch).getAttribute().getName();
String alias = aliasPrefix + '_' + name;
Join copy = target.join(name, ((Fetch) fetch).getJoinType());
copy.alias(alias);
copyJoins(copy, fetch, alias);
joinWhatIsJoinedFromFetches(copy, fetch, alias);
}
}
}

private static boolean hasJoins(From fetch) {
if (!fetch.getJoins().isEmpty()) {
return true;
}
for (Object nested : fetch.getFetches()) {
if (nested instanceof From && hasJoins((From) nested)) {
return true;
}
}
return false;
}

@SuppressWarnings({"rawtypes", "unchecked"})
private static void copyJoins(From target, From source, String aliasPrefix) {
for (Object existing : source.getJoins()) {
Join join = (Join) existing;
String alias = aliasPrefix + '_' + join.getAttribute().getName();
Join copy = target.join(join.getAttribute().getName(), join.getJoinType());
copy.alias(alias);
copyJoins(copy, join, alias);
}
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
Expand Down
Loading
Loading