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]
)
しかし、「whereクエリ」でそれが必要です。
ご参考までに (正確には私も)、クエリのドメインとテストを含む Grails プロジェクトをここから入手できます。