使用grails 2.1.0和默認H2數據庫。我有以下領域:Grails:使用where查詢選擇給定id(子查詢)的兒童父母孩子
class Project {
static hasMany = [tasks: Task]
}
class Task {
Date dateCreated
static belongsTo = [project: Project]
}
我有一個任務ID,並希望得到來自任務的項目(指定ID)的所有任務,採用全新的格姆where queries。這裏是我的嘗試:
def tasks = Task.where {
project == property('project').of { id == firstTask.id }
}.list()
(firstTask.id是給定任務ID,代碼從測試剪斷)
而令人不快的意外結果是:
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
爲什麼!? :(如何形成的不同:
def tasks = Task.findAll() {
dateCreated < property('dateCreated').of { id == secondTask.id }
}
爲了澄清,使用HQL,我想應該是:
def tasks = Task.findAll(
'from Task task where task.project = (select t.project from Task t where t.id = :taskId)',
[taskId: firstTask.id]
)
但我想它在 「裏查詢」
爲您提供方便。 (和我精確),具有域和查詢測試的Grails項目是available here
中項目的獲取類型我爲此提出了一個問題(以及其他一些查詢問題):http://jira.grails.org/browse/GRAILS-9432 –