Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed GRAILS-9804

  • Loading branch information...
commit ffadcb54226bc58d4885d4b47cad6e5459ad21e0 1 parent 1d716e1
@tudor-malene authored
View
13 ...bernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateNamedQueriesBuilder.groovy
@@ -15,6 +15,7 @@
*/
package org.codehaus.groovy.grails.orm.hibernate.cfg
+import grails.gorm.DetachedCriteria
import grails.util.GrailsNameUtils
import java.lang.reflect.Modifier
@@ -214,6 +215,18 @@ class NamedCriteriaProxy<T> {
domainClass.clazz.withCriteria(queryClosure)
}
+ /**
+ * transforms the named query into a detached criteria
+ * Before calling this method, the named criteria parameters must be set
+ * @return
+ */
+ def toDetachedCriteria() {
+ new DetachedCriteria(domainClass.clazz).build {
+ queryBuilder = delegate
+ invokeCriteriaClosure()
+ }
+ }
+
def propertyMissing(String propertyName) {
if (domainClass.metaClass.getMetaProperty(propertyName)) {
def nextInChain = domainClass.metaClass.getMetaProperty(propertyName).getProperty(domainClass)
View
48 ...est-suite-persistence/src/test/groovy/org/codehaus/groovy/grails/orm/hibernate/NamedCriteriaTests.groovy
@@ -895,4 +895,52 @@ class NamedCriteriaTests extends AbstractGrailsHibernateTests {
def pub = NamedCriteriaPublication.recentPublications.findWhere(title: 'Book Number 2')
assertEquals 'Book Number 2', pub.title
}
+
+
+
+// see GRAILS-9804
+ void testDetachedCriteriaFromNamedQuery() {
+ def now = new Date()
+
+ 6.times { cnt ->
+ assert new NamedCriteriaPublication(title: "Some Old Book #${cnt}",
+ datePublished: now - 1000, paperback: true).save(failOnError: true).id
+ assert new NamedCriteriaPublication(title: "Some New Book #${cnt}",
+ datePublished: now, paperback: true).save(failOnError: true).id
+ }
+
+ //test with a list of criteria closures
+ def criteria = NamedCriteriaPublication.publishedAfter(now - 5).toDetachedCriteria().where {
+ eq('paperback', true)
+ }
+ assertEquals 6, criteria.list().size()
+
+ def criteria1 = criteria.where {
+ like('title', '%3')
+ }
+ assertEquals 1, criteria1.list()?.size()
+
+ // test with multiple trailing closures
+ def results = NamedCriteriaPublication.publishedAfter(now - 5).toDetachedCriteria().where{
+ eq 'paperback', true
+ }.where{
+ or {
+ like 'title', '%3'
+ like 'title', '%2'
+ }
+ }.list()
+ assertEquals 2, results?.size()
+
+ results = NamedCriteriaPublication.publishedAfter(now - 5).toDetachedCriteria().where {
+ eq 'paperback', true
+ }.where {
+ or {
+ like 'title', '%3'
+ like 'title', '%2'
+ }
+ }.list([max: 2, offset: 1])
+ assertEquals 1, results?.size()
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.