Skip to content

Commit

Permalink
partial support of gorm DetachedCriteria
Browse files Browse the repository at this point in the history
  • Loading branch information
estradaw committed Jan 16, 2025
1 parent d7a1f09 commit 5ec8d28
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import org.hibernate.SessionFactory;
import org.hibernate.type.BasicTypeReference;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

import org.springframework.orm.hibernate5.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;


import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -128,8 +129,6 @@ public HibernateCriteriaBuilder(Class targetClass, SessionFactory sessionFactory





protected Class getClassForAssociationType(Attribute<?, ?> type) {
if (type instanceof PluralAttribute) {
return ((PluralAttribute)type).getElementType().getJavaType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.grails.orm.hibernate

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import jakarta.persistence.NoResultException
import org.grails.datastore.mapping.reflect.ClassUtils
import org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder
import org.grails.orm.hibernate.cfg.CompositeIdentity
Expand All @@ -13,6 +15,7 @@ import org.grails.datastore.gorm.GormStaticApi
import org.grails.datastore.gorm.finders.DynamicFinder
import org.grails.datastore.gorm.finders.FinderMethod
import org.hibernate.FlushMode
import org.hibernate.NonUniqueResultException
import org.hibernate.Session
import org.hibernate.jpa.QueryHints
import org.hibernate.query.NativeQuery
Expand All @@ -32,6 +35,7 @@ import jakarta.persistence.criteria.Root
* @author Graeme Rocher
* @since 4.0
*/
@Slf4j
@CompileStatic
abstract class AbstractHibernateGormStaticApi<D> extends GormStaticApi<D> {

Expand Down Expand Up @@ -176,17 +180,15 @@ abstract class AbstractHibernateGormStaticApi<D> extends GormStaticApi<D> {
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Long.class)
criteriaQuery.select(criteriaBuilder.count(criteriaQuery.from(persistentEntity.javaClass)))
Query criteria = session.createQuery(criteriaQuery)
HibernateHqlQuery hibernateHqlQuery = new HibernateHqlQuery(
hibernateSession, persistentEntity, criteria) {
@Override
protected void flushBeforeQuery() {
// no-op
}
Long result =0
try {
result = criteria.singleResult as Long
} catch (NonUniqueResultException nonUniqueResultException) {
log.warn(nonUniqueResultException.toString())
} catch (NoResultException noResultException) {
log.warn(noResultException.toString())
}
hibernateTemplate.applySettings(criteria)
def result = hibernateHqlQuery.singleResult()
Number num = result == null ? 0 : (Number)result
return num
return result
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ class HibernateGormStaticApi<D> extends AbstractHibernateGormStaticApi<D> {

@Override
GrailsCriteria createCriteria() {
return null;
// def builder = new HibernateCriteriaBuilder(persistentClass, sessionFactory)
// builder.datastore = (AbstractHibernateDatastore)datastore
// builder.conversionService = conversionService
// return builder
def builder = new HibernateCriteriaBuilder(persistentClass, sessionFactory)
builder.datastore = (AbstractHibernateDatastore)datastore
builder.conversionService = conversionService
return builder
}

@Override
Expand Down
Loading

0 comments on commit 5ec8d28

Please sign in to comment.