Skip to content

Commit ff00b8b

Browse files
related to GRAILS-6140. fix problem related to nested named queries. the problem prevented relationships from being part of the criteria inside of a named query. fixed here.
1 parent 947c34f commit ff00b8b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/java/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateNamedQueriesBuilder.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ class NamedCriteriaProxy {
181181
def nextInChain = domainClass.metaClass.getMetaProperty(methodName).getProperty(domainClass)
182182
nextInChain.previousInChain = this
183183
return nextInChain(args)
184-
} else if (domainClass.metaClass.getMetaProperty(methodName)) {
184+
} else if (domainClass.metaClass.getMetaProperty(methodName) &&
185+
!Collection.isAssignableFrom(domainClass.metaClass.getMetaProperty(methodName).type)) {
185186
def nestedCriteria = domainClass.metaClass.getMetaProperty(methodName).getProperty(domainClass).criteriaClosure.clone()
186187
nestedCriteria.delegate = queryBuilder
187188
nestedCriteria(*args)

src/test/org/codehaus/groovy/grails/orm/hibernate/NamedCriteriaTests.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ class NamedCriteriaTests extends AbstractGrailsHibernateTests {
77

88
protected void onSetUp() {
99
gcl.parseClass('''
10+
class PlantCategory {
11+
Long id
12+
Long version
13+
Set plants
14+
String name
15+
16+
static hasMany = [plants:Plant]
17+
18+
static namedQueries = {
19+
withPlantsInPatch {
20+
plants {
21+
eq 'goesInPatch', true
22+
}
23+
}
24+
}
25+
}
26+
27+
class Plant {
28+
Long id
29+
Long version
30+
boolean goesInPatch
31+
String name
32+
}
1033
1134
class Publication {
1235
Long id
@@ -70,6 +93,23 @@ class Publication {
7093
''')
7194
}
7295

96+
void testNamedQueryWithRelationshipInCriteria() {
97+
def plantCategoryClass = ga.getDomainClass("PlantCategory").clazz
98+
99+
assert plantCategoryClass.newInstance(name:"leafy")
100+
.addToPlants(goesInPatch:true, name:"lettuce")
101+
.save(flush:true)
102+
103+
assert plantCategoryClass.newInstance(name:"grapes")
104+
.addToPlants(goesInPatch:false, name:"white")
105+
.save(flush:true)
106+
107+
session.clear()
108+
109+
def results = plantCategoryClass.withPlantsInPatch.list()
110+
assertEquals 1, results.size()
111+
assertEquals 'leafy', results[0].name
112+
}
73113

74114
void testFindAllWhereAttachedToChainedNamedQueries() {
75115
def publicationClass = ga.getDomainClass("Publication").clazz

0 commit comments

Comments
 (0)