grails - 独立使用GORM使用Domain.findBy [property]时遇到的问题

标签 grails groovy gorm

我正在使用GORM进行项目,根本没有任何Grails,并且在尝试执行findByX时遇到问题。例如,当我尝试

Country.findByName("Canada")



该项目没有编译,它给了我

Error:(80, 9) Groovyc: [Static type checking] - Cannot find matching method app.domain.location.Country#findByName(java.lang.String). Please check if the declared type is right and if the method exists.



即使字段名称存在。

但是,我可以在CountryService.groovy中通过以下方式定义findByName方法:
@Where({ name == countryName })
static Country findByName(String countryName){
    Country.findAll().first()
}

但是我认为这根本不是一个好习惯。

总而言之,是否有依赖项,配置或执行Domain.findByX()之类的GORM查询所需的任何内容?还是无法单独使用GORM?

提前致谢。

最佳答案

您的问题不是GORM方法不存在(最终);而是这是因为它在编译时不存在,并且您似乎正在使用@CompileStatic(或在默认位置配置了项目)。

从Grails文档中(该文档讨论了Gorm和CompileStatic):
https://docs.grails.org/latest/guide/staticTypeCheckingAndCompilation.html

One limitation of using CompileStatic is that when you use it you give up access to the power and flexibility offered by dynamic dispatch. For example, in Grails you would not be able to invoke a GORM dynamic finder from a class that is marked with CompileStatic because the compiler cannot verify that the dynamic finder method exists, because it doesn’t exist at compile time.



如果要使用GORM动态查找器,则需要使用这些查找器为方法/类关闭@CompileStatic。因此,要么删除@CompileStatic批注,要么添加@CompileDynamic
其他相关:
  • When/why would I want to use Groovy's @CompileStatic?
  • http://docs.groovy-lang.org/latest/html/gapi/groovy/transform/CompileStatic.html
  • 关于grails - 独立使用GORM使用Domain.findBy [property]时遇到的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039515/

    相关文章:

    jquery - 将输入下拉列表添加和删除到grails表单

    hibernate - 为什么 Grails 在域对象保存时不通知我错误?

    scala - "Closures"是如何如此强大的抽象,以至于使用它来实现对象系统和基本控制结构?

    grails - Grails条件查询检查 `OR`逻辑

    grails - Grails:无法删除或更新父行:外键约束失败

    grails - 覆盖Grails中的默认无效电子邮件

    tsql - Groovy(Java)在TransactSQL数据库(Microsoft SQL Server 2008 R2)中的存储时间以毫秒为单位

    grails - 在编写转换代码时,有哪些替代方法可以替代 asType() ?

    hibernate - Grails将域类映射到oracle数据库中已经存在的 View

    maven - Grails应用程序仅使用Maven运行,放弃对Grails的支持