This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch merge-hibernate6
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit ad1c0b5e47fb316794dca40fbbd282bd0d11b9df
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Sun Jun 15 20:54:51 2025 -0500

    Fixed Finder Or and And and clone
---
 .../org/grails/orm/hibernate/HibernateSession.java |   6 +-
 .../hibernate/query/AbstractHibernateQuery.java    | 100 ++++++++++++---------
 .../grails/orm/hibernate/query/HibernateQuery.java |  28 +-----
 .../specs/hibernatequery/HibernateQuerySpec.groovy |   3 +-
 .../groovy/grails/gorm/DetachedCriteria.groovy     |   2 +-
 .../query/criteria/AbstractDetachedCriteria.groovy |   2 +-
 6 files changed, 63 insertions(+), 78 deletions(-)

diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
index 8051991a6b..205b3d3a1f 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java
@@ -178,11 +178,7 @@ public class HibernateSession extends 
AbstractHibernateSession {
 
     @Override
     public Query createQuery(Class type, String alias) {
-        final PersistentEntity persistentEntity = 
getMappingContext().getPersistentEntity(type.getName());
-        GrailsHibernateTemplate hibernateTemplate = getHibernateTemplate();
-        Session currentSession = 
hibernateTemplate.getSessionFactory().getCurrentSession();
-        final CriteriaQuery criteria = 
currentSession.getCriteriaBuilder().createQuery(type);
-        return new HibernateQuery(this, persistentEntity);
+        return new HibernateQuery(this, 
getMappingContext().getPersistentEntity(type.getName()));
     }
 
     protected GrailsHibernateTemplate getHibernateTemplate() {
diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java
index 9dec245ed8..60baa295c4 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java
@@ -164,45 +164,36 @@ public abstract class AbstractHibernateQuery extends 
Query {
         }
     }
 
+    /**
+     * This is called for ANDS and is only used by DymamicFinder
+     * It has the limitation of only one OR operator per Query
+     * @param criterion The criterion instance
+     */
     public void add(Criterion criterion) {
-        if (criterion instanceof Between c) {
-           between(c.getProperty(),c.getFrom(),c.getTo());
-        } else if (criterion instanceof Equals c) {
-            eq(c.getProperty(),c.getValue());
-        } else if (criterion instanceof GreaterThanEquals c) {
-            ge(c.getProperty(),c.getValue());
-        } else if (criterion instanceof GreaterThan c) {
-            gt(c.getProperty(),c.getValue());
-        } else if (criterion instanceof IdEquals c) {
-            idEq(c.getValue());
-        } else if (criterion instanceof ILike c) {
-            ilike(c.getProperty(), c.getValue().toString());
-        } else if (criterion instanceof In c) {
-            if (Objects.nonNull(c.getSubquery())) {
-                in(c.getProperty(),c.getSubquery());
-            } else {
-                in(c.getProperty(), c.getValues().stream().toList());
-            }
-        } else if (criterion instanceof IsEmpty c) {
-            isEmpty(c.getProperty());
-        } else if (criterion instanceof IsNotEmpty c) {
-            isNotEmpty(c.getProperty());
-        } else if (criterion instanceof IsNull c) {
-            isNull(c.getProperty());
-        } else if (criterion instanceof IsNotNull c) {
-            isNotNull(c.getProperty());
-        } else if (criterion instanceof RLike c) {
-            rlike(c.getProperty(), c.getValue().toString());
-        } else if (criterion instanceof Like c) {
-            like(c.getProperty(), c.getValue().toString());
-        } else if (criterion instanceof LessThanEquals c) {
-            le(c.getProperty(),c.getValue());
-        } else if (criterion instanceof LessThan c) {
-            lt(c.getProperty(),c.getValue());
-        } else {
-            //TODO It could be that this is the only call needed!
-            detachedCriteria.add(criterion);
-        }
+        Conjunction  conjunction = (Conjunction) detachedCriteria.getCriteria()
+                .stream()
+                .filter(it -> it instanceof Conjunction)
+                .map(Criterion.class::cast)
+                .findFirst()
+                .orElse(new Conjunction());
+        conjunction.add(criterion);
+        detachedCriteria.add(conjunction);
+    }
+
+
+    /**
+     * This is called for ORS and is only used by DymamicFinder
+     * It has the limitation of only one OR operator per Query
+     * @param criterion The criterion instance
+     */
+    public void add(Junction currentJunction, Criterion criterion) {
+        Disjunction disjunction = (Disjunction)  detachedCriteria.getCriteria()
+                .stream()
+                .filter(it -> it instanceof Disjunction)
+                .findFirst()
+                .orElse(new Disjunction());
+        disjunction.add(criterion);
+        detachedCriteria.add(disjunction);
     }
 
     @Override
@@ -225,16 +216,21 @@ public abstract class AbstractHibernateQuery extends 
Query {
 
     @Override
     public Query and(Criterion a, Criterion b) {
-        Closure addClosure = new Closure(this) {
+        and(new Closure(AbstractHibernateQuery.this) {
             public void doCall() {
                 DetachedCriteria owner = (DetachedCriteria) getDelegate();
                 owner.add(Restrictions.and(a,b));
             }
-        };
-        detachedCriteria.and(addClosure);
+        });
+        return this;
+    }
+
+    public Query and(Closure closure) {
+        detachedCriteria.and(closure);
         return this;
     }
 
+
     @Override
     public Query or(Criterion a, Criterion b) {
         Closure orClosure = new Closure(this) {
@@ -244,7 +240,27 @@ public abstract class AbstractHibernateQuery extends Query 
{
             }
         };
         detachedCriteria.or(orClosure);
-           return this;
+        return this;
+    }
+
+
+    public Query or(Closure closure) {
+        detachedCriteria.or(closure);
+        return this;
+    }
+
+    public Query not(Criterion a) {
+        not(new Closure(AbstractHibernateQuery.this) {
+            public void doCall() {
+                ((DetachedCriteria) getDelegate()).add(a);
+            }
+        });
+        return this;
+    }
+
+    public Query not(Closure closure) {
+        detachedCriteria.not(closure);
+        return this;
     }
 
     @Override
diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java
index 2801c1f3a6..a413863030 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java
@@ -118,31 +118,7 @@ public class HibernateQuery extends AbstractHibernateQuery 
{
         return this;
     }
 
-    public Query and(Closure closure) {
-        detachedCriteria.and(closure);
-        return this;
-    }
-
-    public Query or(Closure closure) {
-        detachedCriteria.or(closure);
-        return this;
-    }
 
-    public Query not(Criterion a, Criterion b) {
-        Closure addClosure = new Closure(this) {
-            public void doCall() {
-                DetachedCriteria owner = (DetachedCriteria) getDelegate();
-                owner.add(Restrictions.or(a,b));
-            }
-        };
-        detachedCriteria.not(addClosure);
-        return this;
-    }
-
-    public Query not(Closure closure) {
-        detachedCriteria.not(closure);
-        return this;
-    }
 
     public Query ne(String propertyName, Object propertyValue) {
         detachedCriteria.ne(propertyName, propertyValue);
@@ -243,9 +219,7 @@ public class HibernateQuery extends AbstractHibernateQuery {
             HibernateQuery hibernateQuery = new 
HibernateQuery(hibernateSession, entity);
             hibernateQuery.max(this.max);
             hibernateQuery.offset(this.offset);
-            hibernateQuery.setDetachedCriteria(this.detachedCriteria);
-            this.projections.getProjectionList().forEach(projection -> 
{hibernateQuery.projections().add(projection);});;
-            getCriteria().getCriteria().forEach(hibernateQuery::add);
+            hibernateQuery.setDetachedCriteria(this.detachedCriteria.clone());
             return hibernateQuery;
         });
     }
diff --git 
a/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy
 
b/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy
index bdab037bb1..ac7c85c025 100644
--- 
a/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy
+++ 
b/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/hibernatequery/HibernateQuerySpec.groovy
@@ -237,8 +237,7 @@ class HibernateQuerySpec extends HibernateGormDatastoreSpec 
{
         given:
         new Person(firstName: "Fred", lastName: "Rogers", age: 51).save(flush: 
true)
         Query.Criterion lastNameWrong = new Query.Equals("lastName", "Rogers")
-        Query.Criterion ageIncorrect = new Query.Equals("age", 51)
-        hibernateQuery.not(lastNameWrong, ageIncorrect)
+        hibernateQuery.not(lastNameWrong)
         when:
         def newBob = hibernateQuery.singleResult()
         then:
diff --git 
a/grails-datamapping-core/src/main/groovy/grails/gorm/DetachedCriteria.groovy 
b/grails-datamapping-core/src/main/groovy/grails/gorm/DetachedCriteria.groovy
index 3b5688b461..d317ad4317 100644
--- 
a/grails-datamapping-core/src/main/groovy/grails/gorm/DetachedCriteria.groovy
+++ 
b/grails-datamapping-core/src/main/groovy/grails/gorm/DetachedCriteria.groovy
@@ -719,7 +719,7 @@ class DetachedCriteria<T> extends 
AbstractDetachedCriteria<T> implements GormOpe
     }
 
     @Override
-    protected DetachedCriteria<T> clone() {
+    DetachedCriteria<T> clone() {
         return (DetachedCriteria)super.clone()
     }
 
diff --git 
a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy
 
b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy
index 7fdb442884..fd666a271b 100644
--- 
a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy
+++ 
b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy
@@ -874,7 +874,7 @@ abstract class AbstractDetachedCriteria<T> implements 
Criteria, Cloneable {
 
     @Override
     @CompileStatic
-    protected AbstractDetachedCriteria<T> clone() {
+    AbstractDetachedCriteria<T> clone() {
         AbstractDetachedCriteria criteria = newInstance()
         criteria.@criteria = new ArrayList(this.criteria)
         final projections = new ArrayList(this.projections)

Reply via email to