-
-
Notifications
You must be signed in to change notification settings - Fork 72
Using createAlias in doesn't work in DetachedCriteria #598
Description
Steps to Reproduce
- Create a criteria query that uses a detached criteria as a subquery.
- Use createAlias to create an alias either in the projection or in the restrictions.
Expected Behaviour
It's expected to use the provided alias for the association.
Actual Behaviour
It creates a new alias instead of using the provided alias.
I believe the problem is when GORM is converting the criteria into a Hibernate criterion. In GORM 6 (6.1.12) the AbstractHibernateCriterionAdapter.addAssociationQueryCriterionAdapters
method got the defined alias before calling AbstractHibernateQuery.handleAssociationQuery
and then passed it to it.
GORM 7 doesn't pass the alias so it calls AbstractHibernateCriterionAdapter.handleAssociationQuery(Association<?> association, List<Criterion> criteriaList)
instead of AbstractHibernateCriterionAdapter.handleAssociationQuery(Association<?> association, List<Criterion> criteriaList, String alias)
like in GORM 6.
This causes the references to the created alias to fail as it happens in the example I linked. If I change the alias to the one that the getOrCreateAlias
method generates the query is then generated and everything works (calling curl http://localhost:8080/author/publishers3/1 -H "Accept: application/json
).
Environment Information
- Operating System: Windows 10
- GORM Version: 7.2.1
- Grails Version (if using Grails): 5.1.9
- JDK Version: Oracle 11.0.15
Example Application
https://github.com/daraii/DetachedCriteriaAliasTest
Calling it using curl http://localhost:8080/author/publishers/1 -H "Accept: application/json"
Stack Trace
Stacktrace when using the alias in the restriction:
org.hibernate.HibernateException: Unknown entity: null
https://gist.github.com/daraii/78fc4b4b2730510100dac5ce1222ae87
Stacktrace when using the alias in the projection:
o.g.web.errors.GrailsExceptionResolver : QueryException occurred when processing request: [GET] /author/publishers2/1
could not resolve property: i.publisher of: detachedcriteriaalias.Book. Stacktrace follows:
https://gist.github.com/daraii/3908e6b840b3f1a3b1888716082de85d