使用 grails 2.1.0 和默认 H2 数据库。我有以下域:
class Project {
static hasMany = [tasks: Task]
}
class Task {
Date dateCreated
static belongsTo = [project: Project]
}
我有一个任务 ID,并希望使用全新的 gorm where queries从任务的(给定 ID 的)项目中获取所有任务。这是我的尝试:
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 项目