我有domain
named query:
class Publication {
String title
String author
static namedQueries = {
publicationsWithBookInTitle {
like 'title', '%Book%'
}
}
@Override
String toString() {
return title
}
}
并在controller
我有
def show() {
Publication publicationInstance = Publication.publicationsWithBookInTitle.get()
println "Get: ${publicationInstance}"
println "List: ${Publication.publicationsWithBookInTitle.list()}"
respond publicationInstance
}
和我的unit test
:
void "test show"() {
given:
Publication publication = new Publication(title: 'MyBook', author: 'MB').save(flush: true, failOnError: true)
when:
controller.show()
then:
model.publicationInstance == publication
}
当我运行测试时,它在使用get()方法时给我null并且我的测试失败。系统输出
Get: null
List: [MyBook ]
我搜索了这个,但没有找到这个问题的任何解决方案。
为什么会发生这种情况以及此问题的解决方案或解决方法是什么?
注意: - 我目前正在使用grails 2.3.9
。但我的项目最初是在grails 2.3.5
创建的,然后升级为grails 2.3.9
。
答案 0 :(得分:2)
最后在 dmahapatro 评论的帮助下,我找出了根本原因(@dmahapatro THANKS )。
命名查询会在grails 2.3.9
中显示正确的结果。但我的项目已从使用grails 2.3.5
的{{1}}升级,并且未按grails 2.3.9 release notes中的建议更改为hibernate:3.6.10.6
。一旦我升级了项目hibernate版本,我的问题就解决了。
注意: - 如果您使用hibernate:3.6.10.15
,则会遇到问题。