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 c3d78a25822d1f79e835d552bd993ba2e7abea7f Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Feb 19 21:31:09 2026 -0600 Expand HibernateQuerySpec coverage and fix select projection --- .../grails/orm/HibernateCriteriaBuilder.java | 9 +-- .../grails/orm/hibernate/query/HibernateQuery.java | 16 ++-- .../hibernate/query/HibernateQueryExecutor.java | 3 - .../specs/hibernatequery/HibernateQuerySpec.groovy | 91 +++++++++++++++++++++- 4 files changed, 96 insertions(+), 23 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java index 391f0181c9..9a67dd5235 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java @@ -46,7 +46,6 @@ import org.grails.orm.hibernate.query.HibernateQueryConstants; import org.hibernate.FetchMode; import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.hibernate.transform.ResultTransformer; import org.hibernate.type.BasicTypeReference; import org.hibernate.type.StandardBasicTypes; import org.slf4j.Logger; @@ -433,13 +432,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui } - /** - * Sets the resultTransformer. - * @param transformer The result transformer to use. - */ - public void resultTransformer(ResultTransformer transformer) { - hibernateQuery.setResultTransformer(transformer); - } + /** 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 1507ed21ab..1f5f5e1918 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 @@ -35,10 +35,8 @@ import org.grails.orm.hibernate.proxy.HibernateProxyHandler; import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.criteria.JpaCriteriaQuery; -import org.hibernate.transform.ResultTransformer; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.dao.InvalidDataAccessApiUsageException; @@ -80,7 +78,6 @@ public class HibernateQuery extends Query { private boolean hasJoins = false; protected DetachedCriteria detachedCriteria; protected ProxyHandler proxyHandler = new HibernateProxyHandler(); - protected ResultTransformer resultTransformer; private Integer fetchSize; private Integer timeout; private FlushMode flushMode; @@ -95,13 +92,8 @@ public class HibernateQuery extends Query { this.detachedCriteria = detachedCriteria; } - public void setResultTransformer(ResultTransformer resultTransformer) { - this.resultTransformer = resultTransformer; - } - public ResultTransformer getResultTransformer() { - return resultTransformer; - } + @Override protected Object resolveIdIfEntity(Object value) { @@ -327,7 +319,7 @@ public class HibernateQuery extends Query { @Override public AssociationQuery createQuery(String associationName) { final PersistentProperty property = entity.getPropertyByName(calculatePropertyName(associationName)); - if (property != null && (property instanceof Association)) { + if ((property instanceof Association)) { String alias = generateAlias(associationName); CriteriaAndAlias subCriteria = getOrCreateAlias(associationName, alias); @@ -415,6 +407,8 @@ public class HibernateQuery extends Query { @Override public Query select(String property) { detachedCriteria.select(property); + // Ensure property is added to projections for Hibernate 7 + projections.property(property); return this; } @@ -428,7 +422,7 @@ public class HibernateQuery extends Query { } private HibernateQueryExecutor getHibernateQueryExecutor() { - return new HibernateQueryExecutor(offset, max, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, resultTransformer, proxyHandler); + return new HibernateQueryExecutor(offset, max, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, proxyHandler); } public JpaCriteriaQuery<?> getJpaCriteriaQuery() { diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQueryExecutor.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQueryExecutor.java index 0fa0b59319..be1b6e1a64 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQueryExecutor.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQueryExecutor.java @@ -8,7 +8,6 @@ import org.hibernate.Session; import org.hibernate.query.Query; import org.hibernate.query.ResultListTransformer; import org.hibernate.query.criteria.JpaCriteriaQuery; -import org.hibernate.transform.ResultTransformer; import java.util.List; import java.util.Optional; @@ -22,7 +21,6 @@ public record HibernateQueryExecutor( , Integer timeout , FlushMode flushMode , Boolean readOnly - , ResultTransformer resultTransformer , ProxyHandler proxyHandler ) { @@ -55,7 +53,6 @@ public record HibernateQueryExecutor( Optional.ofNullable(queryCache).ifPresent( qc -> query.setHint("org.hibernate.cacheable", qc)); Optional.ofNullable(maxResults).filter(v -> v > 0).ifPresent(query::setMaxResults); Optional.ofNullable(lockResult).ifPresent(query::setLockMode); - Optional.ofNullable(resultTransformer).ifPresent(query::setResultTransformer); Optional.ofNullable(fetchSize).filter(v -> v > 0).ifPresent(query::setFetchSize); Optional.ofNullable(timeout).filter(v -> v > 0).ifPresent(query::setTimeout); Optional.ofNullable(flushMode).map(mode -> mode.toJpaFlushMode()).ifPresent(query::setFlushMode); 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 d21c8580ec..1126a551b4 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 @@ -810,6 +810,96 @@ class HibernateQuerySpec extends HibernateGormDatastoreSpec { } + def notCriterion() { + given: + new Person(firstName: "Fred", lastName: "Rogers", age: 51).save(flush: true) + hibernateQuery.not(new Query.Equals("firstName", "Fred")) + when: + def newBob = hibernateQuery.singleResult() + then: + oldBob == newBob + } + + def andClosure() { + given: + new Person(firstName: "Bob", lastName: "Builder", age: 51).save(flush: true) + hibernateQuery.and { + eq "lastName", "Builder" + eq "age", 50 + } + when: + def newBob = hibernateQuery.singleResult() + then: + oldBob == newBob + } + + def orClosure() { + given: + new Person(firstName: "Bob", lastName: "Builder", age: 51).save(flush: true) + hibernateQuery.or { + eq "lastName", "Rogers" + eq "age", 50 + } + when: + def newBob = hibernateQuery.singleResult() + then: + oldBob == newBob + } + + def notClosure() { + given: + new Person(firstName: "Fred", lastName: "Rogers", age: 51).save(flush: true) + hibernateQuery.not { + eq "firstName", "Fred" + } + when: + def newBob = hibernateQuery.singleResult() + then: + oldBob == newBob + } + + def firstResult() { + given: + new Person(firstName: "Fred", lastName: "Rogers", age: 52).save(flush: true) + hibernateQuery.firstResult(1).order(Query.Order.asc("age")) + when: + def bobs = hibernateQuery.list() + then: + bobs.size() == 1 + bobs[0].firstName == "Fred" + } + + def select() { + given: + hibernateQuery.select("firstName") + when: + def names = hibernateQuery.list() + then: + names.size() == 1 + names[0] == "Bob" + } + + def sizeNe() { + given: + new Pet(name: "Lucky", age: 48, owner: oldBob).save(flush:true) + hibernateQuery.sizeNe("pets", 0) + when: + def newBob = hibernateQuery.singleResult() + then: + oldBob == newBob + } + + def distinct() { + given: + new Person(firstName: "Bob", lastName: "Builder", age: 50).save(flush: true) + hibernateQuery.projections().distinct("firstName") + when: + def results = hibernateQuery.list() + then: + results.size() == 1 + results[0] == "Bob" + } + } @@ -820,4 +910,3 @@ class BigDecimalEntity implements Serializable { Long version BigDecimal amount } -
