You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/04/09 14:52:45 UTC
(grails-core) 04/40: fix(h7): correct retrieveAll(Iterable) criteria IN predicate and add HibernateSession spec
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 24267ca115e69be794b2d342ff81ce44e28ea07e
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Apr 7 13:44:22 2026 -0500
fix(h7): correct retrieveAll(Iterable) criteria IN predicate and add HibernateSession spec
- Bug: retrieveAll(Class, Iterable) wrapped root.get(id).in(keys) inside
criteriaBuilder.in(), creating an empty IN clause that always returned 0 rows.
Fix: use root.get(id).in(keys) directly as the WHERE predicate.
- Created HibernateSessionSpec.groovy with 31 tests covering: persist, merge,
refresh, delete, lock, flush, clear, contains, getObjectIdentifier,
deleteAll/updateAll criteria queries, retrieveAll (varargs and Iterable),
createQuery, setFlushMode/getFlushMode round-trip, getNativeInterface,
and transaction guard paths.
- Marked HibernateSession as covered in JACOCO_COVERAGE_VIOLATIONS.md
Co-authored-by: Copilot <22...@users.noreply.github.com>
---
JACOCO_COVERAGE_VIOLATIONS.md | 2 +-
.../org/grails/orm/hibernate/HibernateSession.java | 2 +-
.../orm/hibernate/HibernateSessionSpec.groovy | 434 +++++++++++++++++++++
3 files changed, 436 insertions(+), 2 deletions(-)
diff --git a/JACOCO_COVERAGE_VIOLATIONS.md b/JACOCO_COVERAGE_VIOLATIONS.md
index 228d641c15..600114850d 100644
--- a/JACOCO_COVERAGE_VIOLATIONS.md
+++ b/JACOCO_COVERAGE_VIOLATIONS.md
@@ -10,7 +10,7 @@ Generated on: 2026-04-06 16:49:20
| org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateSimpleIdentityProperty | 61.11% | X
| org.grails.orm.hibernate.GrailsSessionContext | 62.50% | X |
| org.grails.orm.hibernate.query.HqlQueryDelegate | 62.50% | X
-| org.grails.orm.hibernate.HibernateSession | 64.77% |
+| org.grails.orm.hibernate.HibernateSession | 64.77% | X |
| org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedPersistentEntity | 64.79% | X
| org.grails.orm.hibernate.cfg.CacheConfig.Include | 65.12% | X
| org.grails.orm.hibernate.query.SelectQueryDelegate | 66.36% | X
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
index 96aaf154be..bcda788db8 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
@@ -387,7 +387,7 @@ public class HibernateSession extends AbstractAttributeStoringSession implements
CriteriaQuery<?> criteriaQuery = criteriaBuilder.createQuery(type);
final Root<?> root = criteriaQuery.from(type);
final String id = persistentEntity.getIdentity().getName();
- criteriaQuery = criteriaQuery.where(criteriaBuilder.in(root.get(id).in(getIterableAsCollection(keys))));
+ criteriaQuery = criteriaQuery.where(root.get(id).in(getIterableAsCollection(keys)));
final org.hibernate.query.Query<?> jpaQuery = session.createQuery(criteriaQuery);
getHibernateTemplate().applySettings(jpaQuery);
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy
new file mode 100644
index 0000000000..b765685fcd
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateSessionSpec.groovy
@@ -0,0 +1,434 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.orm.hibernate
+
+import grails.gorm.DetachedCriteria
+import grails.gorm.annotation.Entity
+import grails.gorm.hibernate.HibernateEntity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import jakarta.persistence.FlushModeType
+import org.grails.orm.hibernate.query.HibernateQuery
+
+class HibernateSessionSpec extends HibernateGormDatastoreSpec {
+
+ def setupSpec() {
+ manager.addAllDomainClasses([HSBook])
+ }
+
+ // -------------------------------------------------------------------------
+ // Accessors and simple state
+ // -------------------------------------------------------------------------
+
+ void "isSchemaless returns false"() {
+ expect:
+ !getSession().isSchemaless()
+ }
+
+ void "isConnected returns true for a fresh session"() {
+ expect:
+ getSession().isConnected()
+ }
+
+ void "disconnect sets connected to false"() {
+ given:
+ def session = getSession()
+
+ when:
+ session.disconnect()
+
+ then:
+ !session.isConnected()
+ }
+
+ void "getMappingContext returns the datastore mapping context"() {
+ expect:
+ getSession().getMappingContext() == datastore.getMappingContext()
+ }
+
+ void "getDatastore returns the HibernateDatastore"() {
+ expect:
+ getSession().getDatastore() == datastore
+ }
+
+ void "getNativeInterface returns the HibernateTemplate"() {
+ given:
+ def session = getSession()
+
+ expect:
+ session.getNativeInterface() == session.getHibernateTemplate()
+ }
+
+ void "getHibernateTemplate returns a non-null template"() {
+ expect:
+ getSession().getHibernateTemplate() != null
+ }
+
+ // -------------------------------------------------------------------------
+ // Transaction guards
+ // -------------------------------------------------------------------------
+
+ void "beginTransaction() throws UnsupportedOperationException"() {
+ when:
+ getSession().beginTransaction()
+
+ then:
+ thrown(UnsupportedOperationException)
+ }
+
+ void "beginTransaction(definition) throws UnsupportedOperationException"() {
+ when:
+ getSession().beginTransaction(null)
+
+ then:
+ thrown(UnsupportedOperationException)
+ }
+
+ void "hasTransaction returns true when a transaction is active"() {
+ expect:
+ getSession().hasTransaction()
+ }
+
+ // -------------------------------------------------------------------------
+ // Flush mode
+ // -------------------------------------------------------------------------
+
+ void "getFlushMode and setFlushMode round-trip correctly"() {
+ given:
+ def session = getSession()
+
+ when:
+ session.setFlushMode(FlushModeType.AUTO)
+
+ then:
+ session.getFlushMode() == FlushModeType.AUTO
+
+ when:
+ session.setFlushMode(FlushModeType.COMMIT)
+
+ then:
+ session.getFlushMode() == FlushModeType.COMMIT
+ }
+
+ // -------------------------------------------------------------------------
+ // Persist and retrieve
+ // -------------------------------------------------------------------------
+
+ void "persist(Object) saves entity and returns id"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Grails in Action")
+
+ when:
+ def id = session.persist(book)
+
+ then:
+ id != null
+ session.contains(book)
+ }
+
+ void "insert(Object) delegates to persist and returns id"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Inserted Book")
+
+ when:
+ def id = session.insert(book)
+
+ then:
+ id != null
+ }
+
+ void "persist(Iterable) persists all entities and returns ids"() {
+ given:
+ def session = getSession()
+ def books = [new HSBook(title: "Book A"), new HSBook(title: "Book B")]
+
+ when:
+ def ids = session.persist(books)
+
+ then:
+ ids.size() == 2
+ ids.every { it != null }
+ }
+
+ void "retrieve returns entity by id"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Retrieved Book")
+ def id = session.persist(book)
+ session.flush()
+
+ when:
+ def found = session.retrieve(HSBook, id)
+
+ then:
+ found != null
+ found.title == "Retrieved Book"
+ }
+
+ void "getObjectIdentifier returns the entity id"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Identified Book")
+ def id = session.persist(book)
+
+ when:
+ def result = session.getObjectIdentifier(book)
+
+ then:
+ result == id
+ }
+
+ // -------------------------------------------------------------------------
+ // Session state management
+ // -------------------------------------------------------------------------
+
+ void "contains returns true for persisted entity"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Contained Book")
+ session.persist(book)
+
+ expect:
+ session.contains(book)
+ }
+
+ void "merge returns the merged entity"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Original")
+ session.persist(book)
+ session.flush()
+ session.clear()
+
+ book.title = "Modified"
+
+ when:
+ def merged = session.merge(book)
+
+ then:
+ merged != null
+ }
+
+ void "refresh reloads entity state from database"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Refreshable")
+ session.persist(book)
+ session.flush()
+
+ when:
+ session.refresh(book)
+
+ then:
+ noExceptionThrown()
+ }
+
+ void "flush executes without error"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Flushed")
+ session.persist(book)
+
+ when:
+ session.flush()
+
+ then:
+ noExceptionThrown()
+ }
+
+ void "clear(Object) evicts entity from session"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Evicted")
+ session.persist(book)
+
+ when:
+ session.clear(book)
+
+ then:
+ !session.contains(book)
+ }
+
+ void "clear() clears the entire session"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Cleared")
+ session.persist(book)
+
+ when:
+ session.clear()
+
+ then:
+ !session.contains(book)
+ }
+
+ void "lock(Object) acquires lock without error"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Locked")
+ session.persist(book)
+ session.flush()
+
+ when:
+ session.lock(book)
+
+ then:
+ noExceptionThrown()
+ }
+
+ // -------------------------------------------------------------------------
+ // Delete
+ // -------------------------------------------------------------------------
+
+ void "delete(Object) removes entity"() {
+ given:
+ def session = getSession()
+ def book = new HSBook(title: "Deleted")
+ def id = session.persist(book)
+ session.flush()
+
+ when:
+ session.delete(book)
+ session.flush()
+
+ then:
+ session.retrieve(HSBook, id) == null
+ }
+
+ void "delete(Iterable) removes all entities"() {
+ given:
+ def session = getSession()
+ def books = [new HSBook(title: "Del A"), new HSBook(title: "Del B")]
+ def ids = session.persist(books)
+ session.flush()
+
+ when:
+ session.delete(books)
+ session.flush()
+
+ then:
+ ids.every { session.retrieve(HSBook, it) == null }
+ }
+
+ // -------------------------------------------------------------------------
+ // Bulk retrieve
+ // -------------------------------------------------------------------------
+
+ void "retrieveAll(type, keys...) returns matching entities"() {
+ given:
+ def session = getSession()
+ def b1 = new HSBook(title: "RA1")
+ def b2 = new HSBook(title: "RA2")
+ def id1 = session.persist(b1)
+ def id2 = session.persist(b2)
+ session.flush()
+
+ when:
+ def results = session.retrieveAll(HSBook, id1, id2)
+
+ then:
+ results.size() == 2
+ }
+
+ void "retrieveAll(type, Iterable<keys>) returns matching entities"() {
+ given:
+ def session = getSession()
+ def b1 = new HSBook(title: "RI1")
+ def b2 = new HSBook(title: "RI2")
+ def id1 = session.persist(b1)
+ def id2 = session.persist(b2)
+ session.flush()
+
+ when:
+ def results = session.retrieveAll(HSBook, [id1, id2])
+
+ then:
+ results.size() == 2
+ }
+
+ // -------------------------------------------------------------------------
+ // Bulk criteria operations
+ // -------------------------------------------------------------------------
+
+ void "deleteAll(criteria) bulk deletes matching entities"() {
+ given:
+ def session = getSession()
+ ['Bulk A', 'Bulk B', 'Keep'].each { title ->
+ session.persist(new HSBook(title: title))
+ }
+ session.flush()
+ session.clear()
+
+ def criteria = new DetachedCriteria(HSBook).build {
+ like('title', 'Bulk%')
+ }
+
+ when:
+ long deleted = session.deleteAll(criteria)
+
+ then:
+ deleted == 2
+ }
+
+ void "updateAll(criteria, properties) bulk updates matching entities"() {
+ given:
+ def session = getSession()
+ ['Update A', 'Update B'].each { title ->
+ session.persist(new HSBook(title: title))
+ }
+ session.flush()
+ session.clear()
+
+ def criteria = new DetachedCriteria(HSBook).build {
+ like('title', 'Update%')
+ }
+
+ when:
+ long updated = session.updateAll(criteria, [title: 'Updated'])
+
+ then:
+ updated == 2
+ }
+
+ // -------------------------------------------------------------------------
+ // Query creation
+ // -------------------------------------------------------------------------
+
+ void "createQuery(type) returns a HibernateQuery"() {
+ when:
+ def query = getSession().createQuery(HSBook)
+
+ then:
+ query instanceof HibernateQuery
+ }
+
+ void "createQuery(type, alias) returns a HibernateQuery with alias set"() {
+ when:
+ def query = getSession().createQuery(HSBook, 'b')
+
+ then:
+ query instanceof HibernateQuery
+ }
+}
+
+@Entity
+class HSBook implements HibernateEntity<HSBook> {
+ String title
+}