使用detachedCriteria作为in子句的子查询

时间:2012-11-07 06:48:26

标签: grails subquery gorm detachedcriteria

我正在尝试使用分离的条件作为'in'子句的子查询但不知何故它不起作用:

def trades = new DetachedCriteria(Trade).build {
            projections { property "tradeNbr" }
}

def activities = Activiies.withCriteria {
            'in' "tradeNumber", trades
}

这是我遇到的错误:

2012-11-07 01:07:09,088 [http-bio-8081-exec-1] TRACE sql.BasicBinder  - f228562 - binding parameter [1] as [INTEGER] - grails.gorm.DetachedCriteria@57c4e7b2



grails.gorm.DetachedCriteria cannot be cast to java.lang.Integer. Stacktrace follows:
java.lang.ClassCastException: grails.gorm.DetachedCriteria cannot be cast to java.lang.Integer
            at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1587)
            at org.grails.datastore.gorm.GormStaticApi.withCriteria(GormStaticApi.groovy:282)

1 个答案:

答案 0 :(得分:2)

我认为list只是默认,因为您可以在each构造中使用它,例如

trades.each {
    println it
}

那是因为它实现了Iterable。否则,您实际上必须使用trades.list()运行查询。只需使用trades即可引用DetachedCriteria对象。

def activities = Activities.withCriteria {
    'in'("tradeNumber", trades.list())
}