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

Commit 8722a83

Browse files
authored
Merge pull request #172 from grails/implicitSubqueryAlias
Create implicit alias for subquery instead of using alias this_
2 parents db9ce85 + 675e229 commit 8722a83

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
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
}

grails-datastore-gorm-hibernate5/src/test/groovy/grails/gorm/tests/DetachCriteriaSubquerySpec.groovy

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,46 @@ class DetachCriteriaSubquerySpec extends GormDatastoreSpec {
3131
def u = User
3232
exists(
3333
GroupAssignment.where {
34+
def ga0 = GroupAssignment
3435
user.id == u.id && group.supervisor.email == supervisorEmail
3536
}.id()
3637
)
3738
}
38-
criteria.list()
39+
List<User> result = criteria.list()
3940

4041
then:
4142
noExceptionThrown()
43+
result.size() == 1
44+
}
45+
46+
void "test that detached criteria subquery should create implicit alias instead of using this_"() {
47+
48+
setup:
49+
User supVisor = createUser('[email protected]')
50+
User user1 = createUser('[email protected]')
51+
User user2 = createUser('[email protected]')
52+
53+
Group group1 = createGroup('Group 1', supVisor)
54+
Group group2 = createGroup('Group 2', supVisor)
55+
56+
assignGroup(user1, group1)
57+
assignGroup(user1, group2)
58+
59+
when:
60+
String supervisorEmail = '[email protected]'
61+
DetachedCriteria<User> criteria = User.where {
62+
def u = User
63+
exists(
64+
GroupAssignment.where {
65+
user.id == u.id && group.supervisor.email == supervisorEmail
66+
}.id()
67+
)
68+
}
69+
List<User> result = criteria.list()
70+
71+
then:
72+
noExceptionThrown()
73+
result.size() == 1
4274
}
4375

4476
private User createUser(String email) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#org.slf4j.simpleLogger.defaultLogLevel=debug
2-
org.slf4j.simpleLogger.log.org.hibernate=trace
2+
#org.slf4j.simpleLogger.log.org.hibernate=trace
3+
#org.slf4j.simpleLogger.log.org.hibernate.SQL=debug

0 commit comments

Comments
 (0)