Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 675e229

Browse files
committed
#171 Implicitly create alias for inner sub-query
to avoid multiple conflicting this_ aliases.
1 parent 47b2bf0 commit 675e229

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

grails-datastore-gorm-hibernate5/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ protected org.hibernate.criterion.DetachedCriteria convertToHibernateCriteria(Qu
207207

208208
public static org.hibernate.criterion.DetachedCriteria getHibernateDetachedCriteria(AbstractHibernateQuery query, QueryableCriteria<?> queryableCriteria) {
209209
String alias = queryableCriteria.getAlias();
210+
return getHibernateDetachedCriteria(query, queryableCriteria, alias);
211+
}
212+
213+
public static org.hibernate.criterion.DetachedCriteria getHibernateDetachedCriteria(AbstractHibernateQuery query, QueryableCriteria<?> queryableCriteria, String alias) {
210214
PersistentEntity persistentEntity = queryableCriteria.getPersistentEntity();
211215
Class targetClass = persistentEntity.getJavaClass();
212216
org.hibernate.criterion.DetachedCriteria detachedCriteria;

grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/AbstractHibernateCriterionAdapter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
public abstract class AbstractHibernateCriterionAdapter {
3737
protected static final Map<Class<?>, CriterionAdaptor<?>> criterionAdaptors = new HashMap<Class<?>, CriterionAdaptor<?>>();
3838
protected static boolean initialized;
39+
protected static final String ALIAS = "_alias";
3940

4041
public AbstractHibernateCriterionAdapter() {
4142
initialize();
@@ -154,7 +155,12 @@ public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Que
154155
criterionAdaptors.put(Query.Exists.class, new CriterionAdaptor<Query.Exists>() {
155156
@Override
156157
public Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, Query.Exists criterion, String alias) {
157-
DetachedCriteria detachedCriteria = toHibernateDetachedCriteria(hibernateQuery,criterion.getSubquery());
158+
final QueryableCriteria subquery = criterion.getSubquery();
159+
String subqueryAlias = subquery.getAlias();
160+
if (subquery.getAlias() == null) {
161+
subqueryAlias = criterion.getSubquery().getPersistentEntity().getJavaClass().getSimpleName() + ALIAS;
162+
}
163+
DetachedCriteria detachedCriteria = toHibernateDetachedCriteria(hibernateQuery,subquery, subqueryAlias);
158164
return Subqueries.exists(detachedCriteria);
159165
}
160166
});
@@ -537,6 +543,10 @@ public org.hibernate.criterion.Criterion toHibernateCriterion(AbstractHibernateQ
537543

538544
protected abstract org.hibernate.criterion.DetachedCriteria toHibernateDetachedCriteria(AbstractHibernateQuery query, QueryableCriteria<?> queryableCriteria);
539545

546+
protected org.hibernate.criterion.DetachedCriteria toHibernateDetachedCriteria(AbstractHibernateQuery query, QueryableCriteria<?> queryableCriteria, String alias) {
547+
return toHibernateDetachedCriteria(query, queryableCriteria);
548+
}
549+
540550
public static abstract class CriterionAdaptor<T extends Query.Criterion> {
541551
public abstract org.hibernate.criterion.Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery, T criterion, String alias);
542552

grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/query/HibernateCriterionAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ protected Criterion createRlikeExpression(String propertyName, String pattern) {
3737
protected DetachedCriteria toHibernateDetachedCriteria(AbstractHibernateQuery hibernateQuery, QueryableCriteria<?> queryableCriteria) {
3838
return HibernateCriteriaBuilder.getHibernateDetachedCriteria(hibernateQuery, queryableCriteria);
3939
}
40+
41+
@Override
42+
protected DetachedCriteria toHibernateDetachedCriteria(AbstractHibernateQuery hibernateQuery, QueryableCriteria<?> queryableCriteria, String alias) {
43+
if (alias == null) {
44+
return toHibernateDetachedCriteria(hibernateQuery, queryableCriteria);
45+
}
46+
return HibernateCriteriaBuilder.getHibernateDetachedCriteria(hibernateQuery, queryableCriteria, alias);
47+
}
4048
}

0 commit comments

Comments
 (0)