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

Commit 4bc262c

Browse files
committed
Fixes grails/grails-data-mapping#1283
Prevent type conversion when criteria is instanceof org.hibernate.criterion.DetachedCriteria.
1 parent 7003deb commit 4bc262c

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ else if (criterion instanceof PropertyCriterion) {
164164
Object value = pc.getValue();
165165
if (value instanceof QueryableCriteria) {
166166
setDetachedCriteriaValue((QueryableCriteria) value, pc);
167-
}
168-
// ignore Size related constraints
169-
else {
170-
doTypeConversionIfNeccessary(getEntity(), pc);
167+
} else {
168+
if (!(value instanceof DetachedCriteria)) {
169+
doTypeConversionIfNeccessary(getEntity(), pc);
170+
}
171171
}
172172
}
173173
if (criterion instanceof DetachedAssociationCriteria) {
@@ -266,6 +266,7 @@ protected String getCurrentAlias() {
266266

267267
@SuppressWarnings("unchecked")
268268
static void doTypeConversionIfNeccessary(PersistentEntity entity, PropertyCriterion pc) {
269+
// ignore Size related constraints
269270
if (pc.getClass().getSimpleName().startsWith(SIZE_CONSTRAINT_PREFIX)) {
270271
return;
271272
}

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DetachCriteriaSubquerySpec extends GormDatastoreSpec {
99

1010
@Override
1111
List getDomainClasses() {
12-
return [User, Group, GroupAssignment]
12+
return [User, Group, GroupAssignment, Organisation]
1313
}
1414

1515
void "test detached associated criteria in subquery"() {
@@ -43,6 +43,30 @@ class DetachCriteriaSubquerySpec extends GormDatastoreSpec {
4343
result.size() == 1
4444
}
4545

46+
void "test executing detached criteria in sub-query multiple times"() {
47+
48+
setup:
49+
Organisation orgA = new Organisation(name: "A")
50+
orgA.addToUsers(email: 'user1@a')
51+
orgA.addToUsers(email: 'user2@a')
52+
orgA.addToUsers(email: 'user3@a')
53+
orgA.save(flush: true)
54+
Organisation orgB = new Organisation(name: "B")
55+
orgB.addToUsers(email: 'user1@b')
56+
orgB.addToUsers(email: 'user2@b')
57+
orgB.save(flush: true)
58+
59+
when:
60+
DetachedCriteria<User> criteria = User.where {
61+
inList('organisation', Organisation.where { name == 'A' || name == 'B' }.id())
62+
}
63+
List<User> result = criteria.list()
64+
result = criteria.list()
65+
66+
then:
67+
result.size() == 5
68+
}
69+
4670
void "test that detached criteria subquery should create implicit alias instead of using this_"() {
4771

4872
setup:
@@ -74,9 +98,11 @@ class DetachCriteriaSubquerySpec extends GormDatastoreSpec {
7498
}
7599

76100
private User createUser(String email) {
77-
User user = new User()
78-
user.email = email
79-
user.save(flush: true)
101+
User user = new User(email: email)
102+
Organisation defaultOrg = Organisation.findOrCreateByName("default")
103+
defaultOrg.addToUsers(user)
104+
defaultOrg.save(flush: true)
105+
user
80106
}
81107

82108
private Group createGroup(String name, User supervisor) {
@@ -99,6 +125,7 @@ class DetachCriteriaSubquerySpec extends GormDatastoreSpec {
99125
@Entity
100126
class User implements HibernateEntity<User> {
101127
String email
128+
static belongsTo = [organisation: Organisation]
102129
static mapping = {
103130
table 'T_USER'
104131
}
@@ -121,3 +148,9 @@ class GroupAssignment implements HibernateEntity<GroupAssignment> {
121148
table 'T_GROUP_ASSIGNMENT'
122149
}
123150
}
151+
152+
@Entity
153+
class Organisation implements HibernateEntity<Organisation> {
154+
String name
155+
static hasMany = [users: User]
156+
}

0 commit comments

Comments
 (0)