Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

GRAILS-9804 #322

Closed
wants to merge 1 commit into from

3 participants

Tudor Malene graemerocher Jeff Scott Brown
Tudor Malene
  • added toDetachedCriteria method to NamedCriteriaProxy
  • added test case
Tudor Malene tudor-malene referenced this pull request
Closed

named queries patch #317

Jeff Scott Brown jeffbrown was assigned
graemerocher
Owner

This code has moved to https://github.com/grails/grails-data-mapping to cannot be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2013
  1. Tudor Malene

    fixed GRAILS-9804

    tudor-malene authored
This page is out of date. Refresh to see the latest.
13 ...bernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateNamedQueriesBuilder.groovy
View
@@ -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)
48 ...est-suite-persistence/src/test/groovy/org/codehaus/groovy/grails/orm/hibernate/NamedCriteriaTests.groovy
View
@@ -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()
+ }
+
+
}
Something went wrong with that request. Please try again.