From 539c0f0e57f597ef3e648f727fe7b2e609e0ed8a Mon Sep 17 00:00:00 2001 From: daraii Date: Mon, 1 Aug 2022 18:08:50 -0400 Subject: [PATCH 1/2] fixes grails/gorm-hibernate5#598 without breaking grails/gorm-hibernate5#171 --- .../hibernate/query/AbstractHibernateCriterionAdapter.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriterionAdapter.java b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriterionAdapter.java index 434ee4212..75ca552cb 100644 --- a/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriterionAdapter.java +++ b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriterionAdapter.java @@ -179,7 +179,12 @@ protected void addAssociationQueryCriterionAdapters() { @Override public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.Criterion criterion, String alias) { DetachedAssociationCriteria existing = (DetachedAssociationCriteria) criterion; - alias = hibernateQuery.handleAssociationQuery(existing.getAssociation(), existing.getCriteria()); + if(existing.getAlias() == null) { + alias = hibernateQuery.handleAssociationQuery(existing.getAssociation(), existing.getCriteria()); + } + else{ + alias = hibernateQuery.handleAssociationQuery(existing.getAssociation(), existing.getCriteria(), existing.getAlias()); + } Association association = existing.getAssociation(); hibernateQuery.associationStack.add(association); Junction conjunction = Restrictions.conjunction(); From 175313149f3692e519ea80010664390b124c764b Mon Sep 17 00:00:00 2001 From: daraii Date: Sun, 18 Sep 2022 20:57:01 -0400 Subject: [PATCH 2/2] Tests for grails/gorm-hibernate5#598. --- ...DetachedCriteriaProjectionAliasSpec.groovy | 69 +++++++++++++++++++ .../DetachedCriteriaProjectionSpec.groovy | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionAliasSpec.groovy diff --git a/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionAliasSpec.groovy b/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionAliasSpec.groovy new file mode 100644 index 000000000..a15e822c7 --- /dev/null +++ b/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionAliasSpec.groovy @@ -0,0 +1,69 @@ +package grails.gorm.tests + +import grails.gorm.DetachedCriteria +import grails.gorm.transactions.Rollback +import grails.gorm.transactions.Transactional +import org.grails.orm.hibernate.HibernateDatastore +import org.springframework.transaction.PlatformTransactionManager +import spock.lang.AutoCleanup +import spock.lang.Issue +import spock.lang.Shared +import spock.lang.Specification + + +class DetachedCriteriaProjectionAliasSpec extends Specification { + + @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(Entity1, Entity2, DetachedEntity) + @Shared PlatformTransactionManager transactionManager = datastore.getTransactionManager() + + @Transactional + def setup() { + DetachedEntity.findAll().each { it.delete() } + Entity1.findAll().each { it.delete(flush: true) } + Entity2.findAll().each { it.delete(flush: true) } + final entity1 = new Entity1(id: 1, field1: 'E1').save() + final entity2 = new Entity2(id: 2, field: 'E2', parent: entity1).save() + entity1.addToChildren(entity2) + new DetachedEntity(id: 1, entityId: entity1.id, field: 'DE1').save() + new DetachedEntity(id: 2, entityId: entity1.id, field: 'DE2').save() + } + + @Rollback + @Issue('https://github.com/grails/gorm-hibernate5/issues/598') + def 'test projection in detached criteria subquery with aliased join and restriction referencing join'() { + setup: + final detachedCriteria = new DetachedCriteria(Entity1).build { + createAlias("children", "e2") + projections{ + property("id") + } + eq("e2.field", "E2") + } + when: + def res = DetachedEntity.withCriteria { + "in"("entityId", detachedCriteria) + } + then: + res.entityId.first() == 1L + } + + + @Rollback + @Issue('https://github.com/grails/gorm-hibernate5/issues/598') + def 'test aliased projection in detached criteria subquery'() { + setup: + final detachedCriteria = new DetachedCriteria(Entity2).build { + createAlias("parent", "e1") + projections{ + property("e1.id") + } + eq("field", "E2") + } + when: + def res = DetachedEntity.withCriteria { + "in"("entityId", detachedCriteria) + } + then: + res.entityId.first() == 2L + } +} \ No newline at end of file diff --git a/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionSpec.groovy b/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionSpec.groovy index 5c657af91..f07eff4b8 100644 --- a/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionSpec.groovy +++ b/grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionSpec.groovy @@ -90,7 +90,7 @@ public class Entity1 { } @Entity class Entity2 { - static belongsTo = { parent: Entity1 } + static belongsTo = [parent: Entity1] String field } @Entity