Skip to content

Commit 7377657

Browse files
committed
fix for GRAILS-10232 "GORM CountBy does not work anymore with sorted mapping"
1 parent 21af7ef commit 7377657

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsHibernateUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ private static void configureDomainClass(SessionFactory sessionFactory, String s
154154
}
155155
}
156156

157+
public static void populateArgumentsForCriteria(GrailsApplication grailsApplication, Class<?> targetClass, Criteria c, Map argMap) {
158+
populateArgumentsForCriteria(grailsApplication, targetClass, c, argMap, true);
159+
}
160+
157161
/**
158162
* Populates criteria arguments for the given target class and arguments map
159163
*
@@ -163,7 +167,7 @@ private static void configureDomainClass(SessionFactory sessionFactory, String s
163167
* @param argMap The arguments map
164168
*/
165169
@SuppressWarnings("rawtypes")
166-
public static void populateArgumentsForCriteria(GrailsApplication grailsApplication, Class<?> targetClass, Criteria c, Map argMap) {
170+
public static void populateArgumentsForCriteria(GrailsApplication grailsApplication, Class<?> targetClass, Criteria c, Map argMap, boolean includeDefaultMapping) {
167171
Integer maxParam = null;
168172
Integer offsetParam = null;
169173
SimpleTypeConverter converter = new SimpleTypeConverter();
@@ -224,7 +228,7 @@ public static void populateArgumentsForCriteria(GrailsApplication grailsApplicat
224228
}
225229
addOrderPossiblyNested(grailsApplication,c, targetClass, sort, order, ignoreCase);
226230
}
227-
else {
231+
else if(includeDefaultMapping){
228232
Mapping m = GrailsDomainBinder.getMapping(targetClass);
229233
if (m != null && !StringUtils.isBlank(m.getSort())) {
230234
addOrderPossiblyNested(grailsApplication, c, targetClass, m.getSort(), m.getOrder(), true);

grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/CountByPersistentMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Object doInHibernate(Session session) throws HibernateException, SQLExcep
6868
final Criteria crit = getCriteria(datastore, application, session, detachedCriteria, additionalCriteria, clazz);
6969
crit.setProjection(Projections.rowCount());
7070
Map argsMap = (arguments.length > 1 && (arguments[1] instanceof Map)) ? (Map) arguments[1] : Collections.EMPTY_MAP;
71-
GrailsHibernateUtil.populateArgumentsForCriteria(application, clazz, crit, argsMap);
71+
GrailsHibernateUtil.populateArgumentsForCriteria(application, clazz, crit, argsMap, false);
7272
populateCriteriaWithExpressions(crit, operator, expressions);
7373
return crit.uniqueResult();
7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.codehaus.groovy.grails.orm.hibernate
18+
19+
import grails.persistence.Entity
20+
import spock.lang.Issue
21+
22+
/**
23+
*/
24+
class CountByWithDefaultSortOrderSpec extends GormSpec{
25+
26+
@Issue('GRAILS-10232')
27+
void "Test that countBy works when there is a default sort order defined" () {
28+
when:"A countBy query is executed"
29+
int count = CountByWithDefaultSortOrder.countByActivate(true)
30+
then:"No exception is thrown and the result is correct"
31+
count == 0
32+
}
33+
@Override
34+
List getDomainClasses() {
35+
[CountByWithDefaultSortOrder]
36+
}
37+
}
38+
39+
@Entity
40+
class CountByWithDefaultSortOrder {
41+
42+
int sortIndex;
43+
String code;
44+
boolean activate = true;
45+
46+
static mapping = {
47+
id generator: 'increment'
48+
code unique: true
49+
sort 'sortIndex'
50+
}
51+
52+
static constraints = {
53+
code nullable: false, blank: false, minSize: 1, unique: true
54+
sortIndex nullable: false
55+
}
56+
}

0 commit comments

Comments
 (0)