Grails - 标准,独家选择

时间:2011-09-06 02:09:46

标签: hibernate grails criteria eager-loading

我有一个包含1对多关系的域类。一个CreditProvider可以有多个Rate个。 我想选择今天没有评定标准查询的所有CreditProvider(或者如果这种方式不可能的话),我被卡住了。

以下是检索今天评定的所有CreditProviders的查询:

def criteria = CreditProvider.createCriteria()
def forgottenCreditProvidersToday = criteria.list {
                rates {
                        between('dateCreated', dateTodayAtMidnight, dateTodayAndNow)
                }
            }
}

我如何选择相反的,也就是说今天尚未评价的CreditProviders? 另外,为什么我不能急于以这种方式获取费率:

fetchMode("rates", FM.EAGER)

非常感谢你的帮助。


修改 监督解决方案:

def criteria = CreditProvider.createCriteria()

        def creditProvidersRatedToday = criteria.list {
            rates {
                    gt('dateCreated', dateTodayAtMidnight)
        }
    order "originalName", "asc"
}
def creditProviders = CreditProvider.findAll()

creditProviders.removeAll(creditProvidersRatedToday)

我愿意接受更优雅的解决方案:)

2 个答案:

答案 0 :(得分:1)

在生成的SQL中,这需要子查询,如

select * from credit_provider
inner join rates on rates.credit_provider_id = credit_provider.id
where not exists 
  (select id from rates where dateCreated between ? and ?)

Grails Criteria不支持子查询。虽然,HQL does support subqueries - 您可以将其转换为HQL。

答案 1 :(得分:-2)

相反通常由“不”来实现,例如

def criteria = CreditProvider.createCriteria()
def forgottenCreditProvidersToday = criteria.list {
    rates {
        not {
            between('dateCreated', dateTodayAtMidnight, dateTodayAndNow)
        }
    }
}

}