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 2025/08/26 09:28:09 UTC
(grails-core) 04/25: consolidate class
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 13f24c8ad1473d25fe9ed9902d1cd047d1aceb03
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sat Aug 9 23:29:33 2025 -0500
consolidate class
---
.../hibernate/query/AbstractHibernateQuery.java | 524 ---------------------
.../grails/orm/hibernate/query/HibernateQuery.java | 476 +++++++++++++++++--
2 files changed, 448 insertions(+), 552 deletions(-)
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
deleted file mode 100644
index 9fa6a32f85..0000000000
--- a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateQuery.java
+++ /dev/null
@@ -1,524 +0,0 @@
-/* Copyright (C) 2011 SpringSource
- *
- * Licensed 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
- *
- * http://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.query;
-
-import grails.gorm.DetachedCriteria;
-import groovy.lang.Closure;
-import jakarta.persistence.criteria.CriteriaQuery;
-import jakarta.persistence.criteria.JoinType;
-import org.grails.datastore.mapping.model.PersistentEntity;
-import org.grails.datastore.mapping.model.PersistentProperty;
-import org.grails.datastore.mapping.model.types.Association;
-import org.grails.datastore.mapping.proxy.ProxyHandler;
-import org.grails.datastore.mapping.query.AssociationQuery;
-import org.grails.orm.hibernate.proxy.HibernateProxyHandler;
-import org.grails.datastore.mapping.query.Query;
-import org.grails.datastore.mapping.query.api.QueryableCriteria;
-import org.grails.orm.hibernate.AbstractHibernateSession;
-import org.grails.orm.hibernate.IHibernateTemplate;
-import org.hibernate.FlushMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-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;
-
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * Bridges the Query API with the Hibernate Criteria API
- *
- * @author Graeme Rocher
- * @since 1.0
- */
-@SuppressWarnings("rawtypes")
-//@Slf4j
-public abstract class AbstractHibernateQuery extends Query {
- public static final String SIZE_CONSTRAINT_PREFIX = "Size";
-
- protected static final String ALIAS = "_alias";
- protected static ConversionService conversionService = new DefaultConversionService();
-
- private static final Map<String, Boolean> JOIN_STATUS_CACHE = new ConcurrentHashMap<String, Boolean>();
-
- protected String alias;
- protected int aliasCount;
- protected Map<String, CriteriaAndAlias> createdAssociationPaths = new HashMap<String, CriteriaAndAlias>();
- protected LinkedList<String> aliasStack = new LinkedList<String>();
- protected LinkedList<PersistentEntity> entityStack = new LinkedList<PersistentEntity>();
- protected LinkedList<Association> associationStack = new LinkedList<Association>();
- protected LinkedList aliasInstanceStack = new LinkedList();
- private boolean hasJoins = false;
- protected DetachedCriteria detachedCriteria;
- protected ProxyHandler proxyHandler = new HibernateProxyHandler();
- protected ResultTransformer resultTransformer;
- private Integer fetchSize;
- private Integer timeout;
- private FlushMode flushMode;
- private Boolean readOnly;
-
- protected AbstractHibernateQuery(AbstractHibernateSession session, PersistentEntity entity) {
- super(session, entity);
- this.detachedCriteria = new DetachedCriteria(entity.getJavaClass());
- }
-
- public void setDetachedCriteria(DetachedCriteria detachedCriteria) {
- this.detachedCriteria = detachedCriteria;
- }
-
- public void setResultTransformer(ResultTransformer resultTransformer) {
- this.resultTransformer = resultTransformer;
- }
-
-
- @Override
- protected Object resolveIdIfEntity(Object value) {
- // for Hibernate queries, the object itself is used in queries, not the id
- return value;
- }
-
-
- @Override
- public Query isEmpty(String property) {
- detachedCriteria.isEmpty(property);
- return this;
- }
-
- @Override
- public Query isNotEmpty(String property) {
- detachedCriteria.isNotEmpty(property);
- return this;
- }
-
- @Override
- public Query isNull(String property) {
- detachedCriteria.isNull(property);
- return this;
- }
-
- @Override
- public Query isNotNull(String property) {
- detachedCriteria.isNotNull(property);
- return this;
- }
-
-
-
- @Override
- public PersistentEntity getEntity() {
- if (!entityStack.isEmpty()) {
- return entityStack.getLast();
- }
- return super.getEntity();
- }
-
-
- private String getAssociationPath(String propertyName) {
- if(propertyName.indexOf('.') > -1) {
- return propertyName;
- }
- else {
-
- StringBuilder fullPath = new StringBuilder();
- for (Association association : associationStack) {
- fullPath.append(association.getName());
- fullPath.append('.');
- }
- fullPath.append(propertyName);
- return fullPath.toString();
- }
- }
-
- /**
- * 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) {
- detachedCriteria.add(criterion);
- }
-
- public void add(DetachedCriteria<?> detachedCriteria) {
- detachedCriteria.add(new Conjunction(detachedCriteria.getCriteria()));
- }
-
-
- /**
- * 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
- public Query eq(String property, Object value) {
- detachedCriteria.eq(property, value);
- return this;
- }
-
- @Override
- public Query idEq(Object value) {
- detachedCriteria.idEq(value);
- return this;
- }
-
- @Override
- public Query gt(String property, Object value) {
- detachedCriteria.gt(property, value);
- return this;
- }
-
- @Override
- public Query and(Criterion a, Criterion b) {
- and(List.of(a,b));
- return this;
- }
-
- public Query and(List<Criterion> criteria) {
- var conjunction = new Conjunction();
- criteria.forEach(conjunction::add);
- detachedCriteria.add(conjunction);
- return this;
- }
-
- public Query and(Closure closure) {
- detachedCriteria.and(closure);
- return this;
- }
-
-
- @Override
- public Query or(Criterion a, Criterion b) {
- or(List.of(a,b));
- return this;
- }
-
- public Query or(List<Criterion> criteria) {
- var disjunction = new Disjunction();
- criteria.forEach(disjunction::add);
- detachedCriteria.add(disjunction);
- 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(List<Criterion> criteria) {
- var conjunction = new Conjunction();
- criteria.forEach(conjunction::add);
- var negation = new Negation();
- negation.add(conjunction);
- detachedCriteria.add(negation);
- return this;
- }
-
-
- public Query not(Closure closure) {
- detachedCriteria.not(closure);
- return this;
- }
-
- @Override
- public Query allEq(Map<String, Object> values) {
- values.forEach((key, value) -> {
- detachedCriteria.eq(key,value);
- });
- return this;
- }
-
- @Override
- public Query ge(String property, Object value) {
- detachedCriteria.ge(property, value);
- return this;
- }
-
- @Override
- public Query le(String property, Object value) {
- detachedCriteria.le(property, value);
- return this;
- }
-
- @Override
- public Query gte(String property, Object value) {
- detachedCriteria.gte(property, value);
- return this;
- }
-
- @Override
- public Query lte(String property, Object value) {
- detachedCriteria.lte(property, value);
- return this;
- }
-
- @Override
- public Query lt(String property, Object value) {
- detachedCriteria.lt(property, value);
- return this;
- }
-
- @Override
- public Query in(String property, List values) {
- detachedCriteria.in(property,values);
- return this;
- }
-
- @Override
- public Query between(String property, Object start, Object end) {
- detachedCriteria.between(property,start,end);
- return this;
- }
-
- @Override
- public Query like(String property, String expr) {
- detachedCriteria.like(property, expr);
- return this;
- }
-
- @Override
- public Query ilike(String property, String expr) {
- detachedCriteria.ilike(property, expr);
- return this;
- }
-
- @Override
- public Query rlike(String property, String expr) {
- throw new UnsupportedOperationException("Needs RLIKE extension");
-// detachedCriteria.rlike(property, expr);
-// return this;
- }
-
-
- //TODO THIS IS USED BY AbstractCriteriaBuilder which is not the parent of
- // AbstractHibernateCriteriaBuilder
- @Override
- public AssociationQuery createQuery(String associationName) {
- final PersistentProperty property = entity.getPropertyByName(calculatePropertyName(associationName));
- if (property != null && (property instanceof Association)) {
- String alias = generateAlias(associationName);
- CriteriaAndAlias subCriteria = getOrCreateAlias(associationName, alias);
-
- Association association = (Association) property;
- if(subCriteria.criteria != null) {
- return new HibernateAssociationQuery(subCriteria.criteria, (AbstractHibernateSession) getSession(), association.getAssociatedEntity(), association, alias);
- }
- }
- throw new InvalidDataAccessApiUsageException("Cannot query association [" + calculatePropertyName(associationName) + "] of entity [" + entity + "]. Property is not an association!");
- }
-
-
- 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);
- }
- 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);
- }
- }
- return subCriteria;
- }
-
-
-
-
- @Override
- public Query firstResult(int offset) {
- offset(offset);
- return this;
- }
-
- @Override
- public Query cache(boolean cache) {
- return super.cache(cache);
- }
-
- @Override
- public Query lock(boolean lock) {
- return super.lock(lock);
- }
-
-
- @Override
- public Query order(Order order) {
- detachedCriteria.order(order);
- return this;
- }
-
- @Override
- public Query join(String property) {
- detachedCriteria.join(property);
- return this;
- }
-
- @Override
- public Query join(String property, JoinType joinType) {
- detachedCriteria.join(property,joinType);
- return this;
- }
-
- @Override
- public Query select(String property) {
- detachedCriteria.select(property);
- return this;
- }
-
- @Override
- public List list() {
- return getHibernateQueryExecutor().list(getCurrentSession(), getJpaCriteriaQuery());
- }
-
- public List list(Session session) {
- return getHibernateQueryExecutor().list(session, getJpaCriteriaQuery());
- }
-
- private HibernateQueryExecutor getHibernateQueryExecutor() {
- return new HibernateQueryExecutor(offset, max, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, resultTransformer, proxyHandler);
- }
-
- public JpaCriteriaQuery<?> getJpaCriteriaQuery() {
- return new JpaCriteriaQueryCreator(projections, getCriteriaBuilder(), entity, detachedCriteria).createQuery();
- }
-
-
- public void setFetchSize(int fetchSize) {
- this.fetchSize = fetchSize;
- }
-
-
- @Override
- protected void flushBeforeQuery() {
- // do nothing
- }
-
- @Override
- public Object singleResult() {
- return getHibernateQueryExecutor().singleResult(getCurrentSession(), getJpaCriteriaQuery());
- }
-
- public Object singleResult(Session session) {
- return getHibernateQueryExecutor().singleResult(session, getJpaCriteriaQuery());
- }
-
- public Object scroll() {
- return getHibernateQueryExecutor().scroll(getCurrentSession(), getJpaCriteriaQuery());
- }
-
- public Object scroll(Session session) {
- return getHibernateQueryExecutor().scroll(session, getJpaCriteriaQuery());
- }
-
-
- private Session getCurrentSession() {
- return getSessionFactory().getCurrentSession();
- }
-
-
- private SessionFactory getSessionFactory() {
- return ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
- }
-
- public HibernateCriteriaBuilder getCriteriaBuilder() {
- return getSessionFactory().getCriteriaBuilder();
- }
-
-
- @Override
- //TODO replace this
- protected List executeQuery(PersistentEntity entity, Junction criteria) {
- return list();
- }
-
- protected String calculatePropertyName(String property) {
- if (alias == null) {
- return property;
- }
- return alias + '.' + property;
- }
-
- protected String generateAlias(String associationName) {
- return calculatePropertyName(associationName) + calculatePropertyName(ALIAS) + aliasCount++;
- }
-
- public Query in(String propertyName, QueryableCriteria<?> subquery) {
- detachedCriteria.inList(propertyName,subquery);
- return this;
- }
-
-
- public void setTimeout(Integer timeout) {
- this.timeout = timeout;
- }
-
- public void setHibernateFlushMode(FlushMode flushMode) {
- this.flushMode = flushMode;
- }
-
- public void setReadOnly(Boolean readOnly) {
- this.readOnly = readOnly;
- }
-
-
-}
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 bb45a7f306..df5d405643 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
@@ -15,22 +15,40 @@
*/
package org.grails.orm.hibernate.query;
-
-
import grails.gorm.DetachedCriteria;
import groovy.lang.Closure;
+import jakarta.persistence.criteria.CriteriaQuery;
+import jakarta.persistence.criteria.JoinType;
+import org.grails.datastore.mapping.model.PersistentEntity;
+import org.grails.datastore.mapping.model.PersistentProperty;
+import org.grails.datastore.mapping.model.types.Association;
+import org.grails.datastore.mapping.proxy.ProxyHandler;
+import org.grails.datastore.mapping.query.AssociationQuery;
import org.grails.datastore.mapping.query.Projections;
import org.grails.datastore.mapping.query.Query;
-import org.grails.datastore.mapping.query.Restrictions;
import org.grails.datastore.mapping.query.api.QueryableCriteria;
import org.grails.orm.hibernate.AbstractHibernateSession;
-
-import org.grails.datastore.mapping.model.PersistentEntity;
import org.grails.orm.hibernate.GrailsHibernateTemplate;
import org.grails.orm.hibernate.HibernateSession;
+import org.grails.orm.hibernate.IHibernateTemplate;
+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.persister.entity.PropertyMapping;
+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;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Bridges the Query API with the Hibernate Criteria API
@@ -39,20 +57,440 @@ import org.hibernate.persister.entity.PropertyMapping;
* @since 1.0
*/
@SuppressWarnings("rawtypes")
-public class HibernateQuery extends AbstractHibernateQuery {
+public class HibernateQuery extends Query {
+
+ public static final String SIZE_CONSTRAINT_PREFIX = "Size";
+
+ protected static final String ALIAS = "_alias";
+ protected static ConversionService conversionService = new DefaultConversionService();
+
+ private static final Map<String, Boolean> JOIN_STATUS_CACHE = new ConcurrentHashMap<String, Boolean>();
+
+ protected String alias;
+ protected int aliasCount;
+ protected Map<String, CriteriaAndAlias> createdAssociationPaths = new HashMap<String, CriteriaAndAlias>();
+ protected LinkedList<String> aliasStack = new LinkedList<String>();
+ protected LinkedList<PersistentEntity> entityStack = new LinkedList<PersistentEntity>();
+ protected LinkedList<Association> associationStack = new LinkedList<Association>();
+ protected LinkedList aliasInstanceStack = new LinkedList();
+ private boolean hasJoins = false;
+ protected DetachedCriteria detachedCriteria;
+ protected ProxyHandler proxyHandler = new HibernateProxyHandler();
+ protected ResultTransformer resultTransformer;
+ private Integer fetchSize;
+ private Integer timeout;
+ private FlushMode flushMode;
+ private Boolean readOnly;
public HibernateQuery(AbstractHibernateSession session, PersistentEntity entity) {
super(session, entity);
+ this.detachedCriteria = new DetachedCriteria(entity.getJavaClass());
+ }
+
+ public void setDetachedCriteria(DetachedCriteria detachedCriteria) {
+ this.detachedCriteria = detachedCriteria;
+ }
+
+ public void setResultTransformer(ResultTransformer resultTransformer) {
+ this.resultTransformer = resultTransformer;
+ }
+
+ @Override
+ protected Object resolveIdIfEntity(Object value) {
+ // for Hibernate queries, the object itself is used in queries, not the id
+ return value;
+ }
+
+ @Override
+ public Query isEmpty(String property) {
+ detachedCriteria.isEmpty(property);
+ return this;
+ }
+
+ @Override
+ public Query isNotEmpty(String property) {
+ detachedCriteria.isNotEmpty(property);
+ return this;
+ }
+
+ @Override
+ public Query isNull(String property) {
+ detachedCriteria.isNull(property);
+ return this;
+ }
+
+ @Override
+ public Query isNotNull(String property) {
+ detachedCriteria.isNotNull(property);
+ return this;
+ }
+
+ @Override
+ public PersistentEntity getEntity() {
+ if (!entityStack.isEmpty()) {
+ return entityStack.getLast();
+ }
+ return super.getEntity();
+ }
+
+ private String getAssociationPath(String propertyName) {
+ if(propertyName.indexOf('.') > -1) {
+ return propertyName;
+ }
+ else {
+ StringBuilder fullPath = new StringBuilder();
+ for (Association association : associationStack) {
+ fullPath.append(association.getName());
+ fullPath.append('.');
+ }
+ fullPath.append(propertyName);
+ return fullPath.toString();
+ }
+ }
+
+ public void add(Criterion criterion) {
+ detachedCriteria.add(criterion);
+ }
+
+ public void add(DetachedCriteria<?> detachedCriteria) {
+ detachedCriteria.add(new Conjunction(detachedCriteria.getCriteria()));
+ }
+
+ 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
+ public Query eq(String property, Object value) {
+ detachedCriteria.eq(property, value);
+ return this;
+ }
+
+ @Override
+ public Query idEq(Object value) {
+ detachedCriteria.idEq(value);
+ return this;
+ }
+
+ @Override
+ public Query gt(String property, Object value) {
+ detachedCriteria.gt(property, value);
+ return this;
+ }
+
+ @Override
+ public Query and(Criterion a, Criterion b) {
+ and(List.of(a,b));
+ return this;
+ }
+
+ public Query and(List<Criterion> criteria) {
+ var conjunction = new Conjunction();
+ criteria.forEach(conjunction::add);
+ detachedCriteria.add(conjunction);
+ return this;
+ }
+
+ public Query and(Closure closure) {
+ detachedCriteria.and(closure);
+ return this;
+ }
+
+ @Override
+ public Query or(Criterion a, Criterion b) {
+ or(List.of(a,b));
+ return this;
+ }
+
+ public Query or(List<Criterion> criteria) {
+ var disjunction = new Disjunction();
+ criteria.forEach(disjunction::add);
+ detachedCriteria.add(disjunction);
+ return this;
+ }
+
+ public Query or(Closure closure) {
+ detachedCriteria.or(closure);
+ return this;
+ }
+
+ public Query not(Criterion a) {
+ not(new Closure(HibernateQuery.this) {
+ public void doCall() {
+ ((DetachedCriteria) getDelegate()).add(a);
+ }
+ });
+ return this;
+ }
+
+ public Query not(List<Criterion> criteria) {
+ var conjunction = new Conjunction();
+ criteria.forEach(conjunction::add);
+ var negation = new Negation();
+ negation.add(conjunction);
+ detachedCriteria.add(negation);
+ return this;
+ }
+
+ public Query not(Closure closure) {
+ detachedCriteria.not(closure);
+ return this;
+ }
+
+ @Override
+ public Query allEq(Map<String, Object> values) {
+ values.forEach((key, value) -> {
+ detachedCriteria.eq(key,value);
+ });
+ return this;
+ }
+
+ @Override
+ public Query ge(String property, Object value) {
+ detachedCriteria.ge(property, value);
+ return this;
+ }
+
+ @Override
+ public Query le(String property, Object value) {
+ detachedCriteria.le(property, value);
+ return this;
+ }
+
+ @Override
+ public Query gte(String property, Object value) {
+ detachedCriteria.gte(property, value);
+ return this;
+ }
+
+ @Override
+ public Query lte(String property, Object value) {
+ detachedCriteria.lte(property, value);
+ return this;
+ }
+
+ @Override
+ public Query lt(String property, Object value) {
+ detachedCriteria.lt(property, value);
+ return this;
+ }
+
+ @Override
+ public Query in(String property, List values) {
+ detachedCriteria.in(property,values);
+ return this;
+ }
+
+ @Override
+ public Query between(String property, Object start, Object end) {
+ detachedCriteria.between(property,start,end);
+ return this;
+ }
+
+ @Override
+ public Query like(String property, String expr) {
+ detachedCriteria.like(property, expr);
+ return this;
+ }
+
+ @Override
+ public Query ilike(String property, String expr) {
+ detachedCriteria.ilike(property, expr);
+ return this;
+ }
+
+ @Override
+ public Query rlike(String property, String expr) {
+ throw new UnsupportedOperationException("Needs RLIKE extension");
+ }
+
+ @Override
+ public AssociationQuery createQuery(String associationName) {
+ final PersistentProperty property = entity.getPropertyByName(calculatePropertyName(associationName));
+ if (property != null && (property instanceof Association)) {
+ String alias = generateAlias(associationName);
+ CriteriaAndAlias subCriteria = getOrCreateAlias(associationName, alias);
+
+ Association association = (Association) property;
+ if(subCriteria.criteria != null) {
+ return new HibernateAssociationQuery(subCriteria.criteria, (AbstractHibernateSession) getSession(), association.getAssociatedEntity(), association, alias);
+ }
+ }
+ throw new InvalidDataAccessApiUsageException("Cannot query association [" + calculatePropertyName(associationName) + "] of entity [" + entity + "]. Property is not an association!");
+ }
+
+ 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);
+ }
+ 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);
+ }
+ }
+ return subCriteria;
+ }
+
+ @Override
+ public Query firstResult(int offset) {
+ offset(offset);
+ return this;
+ }
+
+ @Override
+ public Query cache(boolean cache) {
+ return super.cache(cache);
+ }
+
+ @Override
+ public Query lock(boolean lock) {
+ return super.lock(lock);
+ }
+
+ @Override
+ public Query order(Order order) {
+ detachedCriteria.order(order);
+ return this;
+ }
+
+ @Override
+ public Query join(String property) {
+ detachedCriteria.join(property);
+ return this;
+ }
+
+ @Override
+ public Query join(String property, JoinType joinType) {
+ detachedCriteria.join(property,joinType);
+ return this;
+ }
+
+ @Override
+ public Query select(String property) {
+ detachedCriteria.select(property);
+ return this;
+ }
+
+ @Override
+ public List list() {
+ return getHibernateQueryExecutor().list(getCurrentSession(), getJpaCriteriaQuery());
+ }
+
+ public List list(Session session) {
+ return getHibernateQueryExecutor().list(session, getJpaCriteriaQuery());
+ }
+
+ private HibernateQueryExecutor getHibernateQueryExecutor() {
+ return new HibernateQueryExecutor(offset, max, lockResult, queryCache, fetchSize, timeout, flushMode, readOnly, resultTransformer, proxyHandler);
+ }
+
+ public JpaCriteriaQuery<?> getJpaCriteriaQuery() {
+ return new JpaCriteriaQueryCreator(projections, getCriteriaBuilder(), entity, detachedCriteria).createQuery();
+ }
+
+ public void setFetchSize(Integer fetchSize) {
+ this.fetchSize = fetchSize;
+ }
+
+ @Override
+ protected void flushBeforeQuery() {
+ // do nothing
+ }
+
+ @Override
+ public Object singleResult() {
+ return getHibernateQueryExecutor().singleResult(getCurrentSession(), getJpaCriteriaQuery());
+ }
+
+ public Object singleResult(Session session) {
+ return getHibernateQueryExecutor().singleResult(session, getJpaCriteriaQuery());
+ }
+
+ public Object scroll() {
+ return getHibernateQueryExecutor().scroll(getCurrentSession(), getJpaCriteriaQuery());
+ }
+
+ public Object scroll(Session session) {
+ return getHibernateQueryExecutor().scroll(session, getJpaCriteriaQuery());
+ }
+
+ private Session getCurrentSession() {
+ return getSessionFactory().getCurrentSession();
+ }
+
+ private SessionFactory getSessionFactory() {
+ return ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
+ }
+
+ public HibernateCriteriaBuilder getCriteriaBuilder() {
+ return getSessionFactory().getCriteriaBuilder();
+ }
+
+ @Override
+ protected List executeQuery(PersistentEntity entity, Junction criteria) {
+ return list();
+ }
+
+ protected String calculatePropertyName(String property) {
+ if (alias == null) {
+ return property;
+ }
+ return alias + '.' + property;
+ }
+
+ protected String generateAlias(String associationName) {
+ return calculatePropertyName(associationName) + calculatePropertyName(ALIAS) + aliasCount++;
+ }
+
+ public Query in(String propertyName, QueryableCriteria<?> subquery) {
+ detachedCriteria.inList(propertyName,subquery);
+ return this;
+ }
+
+ public void setTimeout(Integer timeout) {
+ this.timeout = timeout;
+ }
+
+ public void setHibernateFlushMode(FlushMode flushMode) {
+ this.flushMode = flushMode;
+ }
+
+ public void setReadOnly(Boolean readOnly) {
+ this.readOnly = readOnly;
}
protected PropertyMapping getEntityPersister(String name, SessionFactory sessionFactory) {
return (PropertyMapping) ((SharedSessionContractImplementor) sessionFactory).getEntityPersister(name,this.entity);
}
- /**
- * TODO FIX THIS
- * @return The hibernate criteria
- */
public DetachedCriteria getHibernateCriteria() {
return detachedCriteria;
}
@@ -72,7 +510,6 @@ public class HibernateQuery extends AbstractHibernateQuery {
return this;
}
-
public Query gtAll(String propertyName, QueryableCriteria<?> subquery) {
detachedCriteria.gtAll(propertyName,subquery);
return this;
@@ -118,8 +555,6 @@ public class HibernateQuery extends AbstractHibernateQuery {
return this;
}
-
-
public Query ne(String propertyName, Object propertyValue) {
detachedCriteria.ne(propertyName, propertyValue);
return this;
@@ -140,7 +575,6 @@ public class HibernateQuery extends AbstractHibernateQuery {
return this;
}
-
public Query geProperty(String propertyName, String otherPropertyName) {
detachedCriteria.geProperty(propertyName,otherPropertyName);
return this;
@@ -161,31 +595,26 @@ public class HibernateQuery extends AbstractHibernateQuery {
return this;
}
-
public Query sizeGt(String propertyName, int size) {
detachedCriteria.sizeGt(propertyName, size);
return this;
}
-
public Query sizeGe(String propertyName, int size) {
detachedCriteria.sizeGe(propertyName, size);
return this;
}
-
public Query sizeLe(String propertyName, int size) {
detachedCriteria.sizeLe(propertyName, size);
return this;
}
-
public Query sizeLt(String propertyName, int size) {
detachedCriteria.sizeLt(propertyName, size);
return this;
}
-
public Query sizeNe(String propertyName, int size) {
detachedCriteria.sizeNe(propertyName, size);
return this;
@@ -196,19 +625,11 @@ public class HibernateQuery extends AbstractHibernateQuery {
return this;
}
-
-
-
public Query distinct() {
projections.add(Projections.distinct());
return this;
}
-
-
-
-
- //TODO verify this is complete
@Override
public Object clone() {
final HibernateSession hibernateSession = (HibernateSession) getSession();
@@ -221,5 +642,4 @@ public class HibernateQuery extends AbstractHibernateQuery {
return hibernateQuery;
});
}
-
}