Grails: selecting Children of Parent of Child with given id (subquery) with a Where Query

Using grails 2.1.0 and default H2 database. I've got the following domains:

class Project {
    static hasMany = [tasks: Task]
}

class Task {
    Date dateCreated
    static belongsTo = [project: Project]
}

I've got a task id and want to get all tasks from the task's (of the given id) project, using the all-new gorm where queries. Here's my attempt:

def tasks = Task.where {
    project == property('project').of { id == firstTask.id }
}.list()

(firstTask.id is the given task id, the code is snipped from a test)

And the unpleasant unexpected result is:

IllegalArgumentException occurred calling getter of so.q.grom.subqueries.Project.id
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of so.q.grom.subqueries.Project.id
    at grails.gorm.DetachedCriteria.list_closure2(DetachedCriteria.groovy:639)
    at grails.gorm.DetachedCriteria.withPopulatedQuery_closure9(DetachedCriteria.groovy:890)
    at org.grails.datastore.gorm.GormStaticApi.withDatastoreSession_closure18(GormStaticApi.groovy:555)
    at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:301)
    at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34)
    at org.grails.datastore.gorm.GormStaticApi.withDatastoreSession(GormStaticApi.groovy:554)
    at grails.gorm.DetachedCriteria.withPopulatedQuery(DetachedCriteria.groovy:873)
    at grails.gorm.DetachedCriteria.list(DetachedCriteria.groovy:638)
    at grails.gorm.DetachedCriteria.list(DetachedCriteria.groovy:637)
    at GormSubqueriesSpec.should get tasks from the same project(GormSubqueriesSpec.groovy:32)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    ... 10 more

Why!? :( How's that different from:

def tasks = Task.findAll() {
    dateCreated < property('dateCreated').of { id == secondTask.id }
}

To clarify, using HQL, what I want would be:

def tasks = Task.findAll(
        'from Task task where task.project = (select t.project from Task t where t.id = :taskId)',
        [taskId: firstTask.id]
)

But I want it in "where queries".

For Your convenience (and me being precise), a Grails project with the domains and tests for the queries is available here

Answers


You should raise this as an issue. The following example compiles and runs:

def tasks = Task.where {
    project == property('project')
}.list()

but adding a sub-query causes problems. At the very least, the error message should be more informative. But I don't see why it couldn't work in theory.

Anyway, in the meantime HQL is probably your best bet.


Need Your Help

Sync with iCal through Sync Services

cocoa core-data service sync icalendar

I'm looking for sample code on how to sync with ical through sync services. Or anything that can point me to the right direction on how to do it. I've already gotten sync services up and running fo...

Plus not working in Scala interpreter

scala interpreter

I am trying to sum a list using fold in the Scala interpreter, but it keeps giving me a strange error. When I type this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.