Skip to content

Commit f69bcde

Browse files
GRAILS-6275 - named queries now support listDistinct()
1 parent ff00b8b commit f69bcde

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package org.codehaus.groovy.grails.orm.hibernate.cfg
1717

1818
import org.codehaus.groovy.grails.orm.hibernate.metaclass.*
19-
import org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport;
19+
import org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport
20+
import org.hibernate.criterion.CriteriaSpecification
2021

2122
/**
2223
* A builder that implements the ORM named queries DSL
@@ -90,24 +91,35 @@ class NamedCriteriaProxy {
9091
crit()
9192
}
9293

93-
def list(Object[] params, Closure additionalCriteriaClosure = null) {
94+
private listInternal(Object[] params, Closure additionalCriteriaClosure, Boolean isDistinct) {
9495
def listClosure = {
9596
queryBuilder = delegate
96-
invokeCriteriaClosure(additionalCriteriaClosure)
97-
def paramsMap
98-
if (params && params[-1] instanceof Map) {
99-
paramsMap = params[-1]
100-
}
101-
if (paramsMap?.max) {
97+
invokeCriteriaClosure(additionalCriteriaClosure)
98+
def paramsMap
99+
if (params && params[-1] instanceof Map) {
100+
paramsMap = params[-1]
101+
}
102+
if (paramsMap?.max) {
102103
maxResults(paramsMap.max)
103104
}
104105
if (paramsMap?.offset) {
105106
firstResult paramsMap.offset
106107
}
108+
if(isDistinct) {
109+
resultTransformer = CriteriaSpecification.DISTINCT_ROOT_ENTITY
110+
}
107111
}
108112
domainClass.clazz.withCriteria(listClosure)
109113
}
110114

115+
def list(Object[] params, Closure additionalCriteriaClosure = null) {
116+
listInternal params, additionalCriteriaClosure, false
117+
}
118+
119+
def listDistinct(Object[] params, Closure additionalCriteriaClosure = null) {
120+
listInternal params, additionalCriteriaClosure, true
121+
}
122+
111123
def call(Object[] params) {
112124
if(params && params[-1] instanceof Closure) {
113125
def additionalCriteriaClosure = params[-1]

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,65 @@ class Publication {
111111
assertEquals 'leafy', results[0].name
112112
}
113113

114+
void testListDistinct() {
115+
def plantCategoryClass = ga.getDomainClass("PlantCategory").clazz
116+
117+
assert plantCategoryClass.newInstance(name:"leafy")
118+
.addToPlants(goesInPatch:true, name:"lettuce")
119+
.addToPlants(goesInPatch:true, name:"cabbage")
120+
.save(flush:true)
121+
122+
assert plantCategoryClass.newInstance(name:"orange")
123+
.addToPlants(goesInPatch:true, name:"carrots")
124+
.addToPlants(goesInPatch:true, name:"pumpkin")
125+
.save(flush:true)
126+
127+
assert plantCategoryClass.newInstance(name:"grapes")
128+
.addToPlants(goesInPatch:false, name:"red")
129+
.addToPlants(goesInPatch:false, name:"white")
130+
.save(flush:true)
131+
132+
session.clear()
133+
134+
def categories = plantCategoryClass.withPlantsInPatch().listDistinct()
135+
136+
assertEquals 2, categories.size()
137+
def names = categories*.name
138+
assertEquals 2, names.size()
139+
assertTrue 'leafy' in names
140+
assertTrue 'orange' in names
141+
}
142+
143+
void testListDistinct2() {
144+
def plantCategoryClass = ga.getDomainClass("PlantCategory").clazz
145+
146+
assert plantCategoryClass.newInstance(name:"leafy")
147+
.addToPlants(goesInPatch:true, name:"lettuce")
148+
.addToPlants(goesInPatch:true, name:"cabbage")
149+
.save(flush:true)
150+
151+
assert plantCategoryClass.newInstance(name:"orange")
152+
.addToPlants(goesInPatch:true, name:"carrots")
153+
.addToPlants(goesInPatch:true, name:"pumpkin")
154+
.save(flush:true)
155+
156+
assert plantCategoryClass.newInstance(name:"grapes")
157+
.addToPlants(goesInPatch:false, name:"red")
158+
.addToPlants(goesInPatch:true, name:"white")
159+
.save(flush:true)
160+
161+
session.clear()
162+
163+
def categories = plantCategoryClass.withPlantsInPatch.listDistinct()
164+
165+
assertEquals 3, categories.size()
166+
def names = categories*.name
167+
assertEquals 3, names.size()
168+
assertTrue 'leafy' in names
169+
assertTrue 'orange' in names
170+
assertTrue 'grapes' in names
171+
}
172+
114173
void testFindAllWhereAttachedToChainedNamedQueries() {
115174
def publicationClass = ga.getDomainClass("Publication").clazz
116175
def now = new Date()

0 commit comments

Comments
 (0)