为什么@GrailsCompileStatic 允许调用动态 GORM 查找器而不是 domainClass.withTransaction()?
Why does @GrailsCompileStatic allow calling dynamic GORM finders but not domainClass.withTransaction()?
将 @GrailsCompileStatic 注释添加到方法或 class 允许使用动态 GORM 查找器,例如findAllByIdAndProperty()。
但是,添加注释不允许 domainClass.withTransaction(),这也是 GORM AST 添加。为什么?
(使用 grails-2.5.3)
更新(05/10/16 - 10:25AM)
@jeff-scott-brown 是对的,它确实可以正常工作,所以这里是失败的代码 @GrailsCompileStatic:
...
RestfulApiService service = ServiceUtils.getService(resourceName)
service.resourceClass.withTransaction { /* do something */ }
(资源Class类型为Class)
错误:
Compilation error: startup failed:
C:\...\myfile.groovy: 100: [Static type checking] - Cannot find matching method java.lang.Class#withTransaction(groovy.lang.Closure). Please check if the declared type is right and if the method exists.
@ line 100, column 13.
service.resourceClass.withTransaction {
^
为什么在这种情况下添加注释后 withTransaction() 会失败?
Why does @GrailsCompileStatic allow calling dynamic GORM finders but
not domainClass.withTransaction()?
确实如此。 https://github.com/jeffbrown/withtx/blob/master/grails-app/controllers/demo/DemoController.groovy 处的代码编译。
import grails.compiler.GrailsCompileStatic
@GrailsCompileStatic
class DemoController {
def index() {
Person.withTransaction {
// ...
}
render 'Success!'
}
}
将 @GrailsCompileStatic 注释添加到方法或 class 允许使用动态 GORM 查找器,例如findAllByIdAndProperty()。 但是,添加注释不允许 domainClass.withTransaction(),这也是 GORM AST 添加。为什么?
(使用 grails-2.5.3)
更新(05/10/16 - 10:25AM) @jeff-scott-brown 是对的,它确实可以正常工作,所以这里是失败的代码 @GrailsCompileStatic:
...
RestfulApiService service = ServiceUtils.getService(resourceName)
service.resourceClass.withTransaction { /* do something */ }
(资源Class类型为Class)
错误:
Compilation error: startup failed:
C:\...\myfile.groovy: 100: [Static type checking] - Cannot find matching method java.lang.Class#withTransaction(groovy.lang.Closure). Please check if the declared type is right and if the method exists.
@ line 100, column 13.
service.resourceClass.withTransaction {
^
为什么在这种情况下添加注释后 withTransaction() 会失败?
Why does @GrailsCompileStatic allow calling dynamic GORM finders but not domainClass.withTransaction()?
确实如此。 https://github.com/jeffbrown/withtx/blob/master/grails-app/controllers/demo/DemoController.groovy 处的代码编译。
import grails.compiler.GrailsCompileStatic
@GrailsCompileStatic
class DemoController {
def index() {
Person.withTransaction {
// ...
}
render 'Success!'
}
}