This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit bcdc6a9a3c92e5cc52499ee265daf18d73921edf Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Feb 20 08:29:46 2026 -0600 Complete HibernateQuery implementation and tests --- .../grails/orm/hibernate/query/HibernateQuery.java | 29 +--- .../specs/hibernatequery/HibernateQuerySpec.groovy | 149 +++++++++++++++++++++ 2 files changed, 156 insertions(+), 22 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java index 4f7cf8dd2d..fbc6e15c82 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java @@ -317,33 +317,18 @@ public class HibernateQuery extends Query { private CriteriaAndAlias getOrCreateAlias(String associationName, String alias) { CriteriaAndAlias subCriteria = null; String associationPath = getAssociationPath(associationName); - CriteriaQuery parentCriteria = getCriteriaBuilder().createQuery(entity.getJavaClass()); if(alias == null) { alias = generateAlias(associationName); } - else { - CriteriaAndAlias criteriaAndAlias = createdAssociationPaths.get(alias); - if(criteriaAndAlias != null) { - parentCriteria = criteriaAndAlias.criteria; - if(parentCriteria != null) { - alias = associationName + '_' + alias; - associationPath = criteriaAndAlias.associationPath + '.' + associationPath; - } - } - } - if (createdAssociationPaths.containsKey(associationName)) { - subCriteria = createdAssociationPaths.get(associationName); + + if (createdAssociationPaths.containsKey(associationPath)) { + subCriteria = createdAssociationPaths.get(associationPath); } else { - JoinType joinType = joinTypes.get(associationName); - if(parentCriteria != null) { -// Criteria sc = parentCriteria.createAlias(associationPath, alias, resolveJoinType(joinType)); -// subCriteria = new CriteriaAndAlias(sc, alias, associationPath); - } - if(subCriteria != null) { - createdAssociationPaths.put(associationPath,subCriteria); - createdAssociationPaths.put(alias,subCriteria); - } + CriteriaQuery criteriaQuery = getCriteriaBuilder().createQuery(entity.getJavaClass()); + subCriteria = new CriteriaAndAlias(criteriaQuery, alias, associationPath); + createdAssociationPaths.put(associationPath, subCriteria); + createdAssociationPaths.put(alias, subCriteria); } return subCriteria; } diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy index d3dd27835c..d97fc5f0b4 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy @@ -911,6 +911,155 @@ class HibernateQuerySpec extends HibernateGormDatastoreSpec { results.size() == 1 results[0] == "Bob" } + + def countMethod() { + given: + new Person(firstName: "Fred", lastName: "Rogers", age: 51).save(flush: true) + hibernateQuery.count() + when: + def count = hibernateQuery.singleResult() + then: + count == 2 + } + + def addCriterion() { + given: + hibernateQuery.add(new Query.Equals("firstName", "Bob")) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def addDetachedCriteria() { + given: + hibernateQuery.add(new DetachedCriteria(Person).eq("firstName", "Bob")) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def addJunctionCriterion() { + given: + hibernateQuery.add(new Query.Disjunction(), new Query.Equals("firstName", "Bob")) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def andList() { + given: + hibernateQuery.and([new Query.Equals("firstName", "Bob"), new Query.Equals("age", 50)]) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def orList() { + given: + hibernateQuery.or([new Query.Equals("firstName", "Fred"), new Query.Equals("firstName", "Bob")]) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def notList() { + given: + new Person(firstName: "Fred", lastName: "Rogers", age: 51).save(flush: true) + hibernateQuery.not([new Query.Equals("firstName", "Fred")]) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def lock() { + given: + hibernateQuery.eq("firstName", "Bob").lock(true) + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def cloneQuery() { + given: + hibernateQuery.eq("firstName", "Bob").max(10).offset(5) + when: + HibernateQuery cloned = (HibernateQuery) hibernateQuery.clone() + then: + cloned != hibernateQuery + cloned.max == hibernateQuery.max + cloned.offset == hibernateQuery.offset + cloned.hibernateCriteria != null + } + + def queryArguments() { + given: + hibernateQuery.setFetchSize(100) + hibernateQuery.setTimeout(10) + hibernateQuery.setHibernateFlushMode(org.hibernate.FlushMode.COMMIT) + hibernateQuery.setReadOnly(true) + hibernateQuery.eq("firstName", "Bob") + when: + def bob = hibernateQuery.singleResult() + then: + bob == oldBob + } + + def listWithSession() { + given: + hibernateQuery.eq("firstName", "Bob") + when: + def session = sessionFactory.openSession() + def results = hibernateQuery.list(session) + session.close() + then: + results.size() == 1 + results[0] == oldBob + } + + def singleResultWithSession() { + given: + hibernateQuery.eq("firstName", "Bob") + when: + def session = sessionFactory.openSession() + def result = hibernateQuery.singleResult(session) + session.close() + then: + result == oldBob + } + + def scroll() { + given: + hibernateQuery.eq("firstName", "Bob") + when: + def scroll = hibernateQuery.scroll() + then: + scroll != null + } + + def equalsAllQueryable() { + given: + new Pet(name: "Lucky", age: 50, owner: oldBob).save(flush:true) + hibernateQuery.eqAll("age", new DetachedCriteria(Pet).eq("name", "Lucky").property("age")) + when: + def result = hibernateQuery.singleResult() + then: + result == oldBob + } + + def testCreateQuery() { + when: + def associationQuery = hibernateQuery.createQuery("pets") + then: + associationQuery != null + associationQuery.getEntity() != null + } }
