我有以下测试
answer = Author.withCriteria {
books {
gt 'price', new DetachedCriteria(Book).build {
projections {
avg 'price'
}
}
}
}
assert answer.size() == 1
Intellij IDEA 毫无例外地执行此测试。如果我运行gradle build
这个测试会引发org.hibernate.exception.SQLGrammarException: could not execute query
异常。
Intellij 生成以下 SQL:
select this_.id as id2_1_, this_.version as version2_1_, this_.age as age2_1_, this_.email as email2_1_, this_.first_name as first5_2_1_, this_.home_page as home6_2_1_, this_.last_name as last7_2_1_, this_.login as login2_1_, this_.salary as salary2_1_, this_.sex as sex2_1_, (SELECT count(*) FROM BOOK b WHERE b.author_id = this_.id) as formula0_1_, books_alia1_.id as id1_0_, books_alia1_.version as version1_0_, books_alia1_.author_id as author3_1_0_, books_alia1_.date_created as date4_1_0_, books_alia1_.last_updated as last5_1_0_, books_alia1_.price as price1_0_, books_alia1_.title as title1_0_ from author this_ inner join book books_alia1_ on this_.id=books_alia1_.author_id where (books_alia1_.price > (select avg(cast(this_.price as double)) as y0_ from book this_))
摇篮SQL:
select this_.id as id2_1_, this_.version as version2_1_, this_.age as age2_1_, this_.email as email2_1_, this_.first_name as first5_2_1_, this_.home_page as home6_2_1_, this_.last_name as last7_2_1_, this_.login as login2_1_, this_.salary as salary2_1_, this_.sex as sex2_1_, (SELECT count(*) FROM BOOK b WHERE b.author_id = this_.id) as formula0_1_, books_alia1_.id as id0_0_, books_alia1_.version as version0_0_, books_alia1_.author_id as author3_0_0_, books_alia1_.date_created as date4_0_0_, books_alia1_.last_updated as last5_0_0_, books_alia1_.price as price0_0_, books_alia1_.title as title0_0_ from author this_ inner join book books_alia1_ on this_.id=books_alia1_.author_id where (books_alia1_.price > (select from book this_)
您可以看到avg 'price'
零件的问题。
问题是一样的:为什么 Gradle 执行的测试有异常?
ps Intellij 依赖项是通过gradle idea
命令安装的