2

I am trying to use a detached criteria as subquery for 'in' clause but somehow it does not work:

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

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

This is the error that I am running into:

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)
rks
  • 451
  • 1
  • 8
  • 16
  • The documentation for the detachedCriteria says that list() is a default method but apparently it is not doing it. I was expecting 'print trades' to get the trades but it prints the object reference. Am I doing something wrong? – rks Nov 07 '12 at 15:06

1 Answers1

2

I believe that list is only a default in the sense that you can use it in an each construct like

trades.each {
    println it
}

And that's because it implements Iterable. Otherwise you actually have to run the query with trades.list(). Just using trades refers to the DetachedCriteria object.

def activities = Activities.withCriteria {
    'in'("tradeNumber", trades.list())
}
Kelly
  • 3,709
  • 4
  • 20
  • 31
  • trades.list() should work but I was hoping that the query parser would parse the trades criteria within activities and at that time expand the query within activities and call list by default. – rks Nov 08 '12 at 16:10