【发布时间】:2015-05-27 13:11:38
【问题描述】:
我正在尝试在我的域类中使用服务进行自定义验证。在其他领域类中,这工作得很好,但在这里我收到一个 Hibernate 断言错误。
域类自定义验证:
end validator: { val, obj ->
if (obj.userWorkingTimeRegulationService.validateit() == false){
return ["error.workingregulation"]
}
我使用以下方法注入服务:
def userWorkingTimeRegulationService
该服务仅返回 true 用于测试目的:
package usermanagement
import grails.transaction.Transactional
@Transactional
class UserWorkingTimeRegulationService {
def serviceMethod() {
}
def validateit(){
return true
}
}
堆栈跟踪:
| Error 2015-05-27 15:07:54,909 [localhost-startStop-1] ERROR hibernate.AssertionFailure - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in usermanagement.UserWorkingTimeRegulation entry (don't flush the Session after an exception occurs)
| Error 2015-05-27 15:07:54,927 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener - Error initializing the application: null id in usermanagement.UserWorkingTimeRegulation entry (don't flush the Session after an exception occurs)
Message: null id in usermanagement.UserWorkingTimeRegulation entry (don't flush the Session after an exception occurs)
Line | Method
->> 24 | doCall in usermanagement.UserWorkingTimeRegulation$__clinit__closure4$_closure8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 47 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
| 52 | create . . . . . . . . . . . . . in usermanagement.UserWorkingTimeRegulation
| 47 | doCall in BootStrap$_closure1
| 327 | evaluateEnvironmentSpecificBlock in grails.util.Environment
| 320 | executeForEnvironment in ''
| 296 | executeForCurrentEnvironment . . in ''
| 266 | run in java.util.concurrent.FutureTask
| 1142 | runWorker . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor
| 617 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run . . . . . . . . . . . . . . in java.lang.Thread
Error |
Forked Grails VM exited with error
如果我使用在域类中声明的方法都可以,那肯定是服务有问题。在另一个域类中,我在自定义验证中使用服务没有问题,我不知道这个有什么特别之处。
编辑:
我在我的Bootstrap.groovy 中创建了一个 UserWorkingTimeRegulation,如果我在我的域类中注释自定义验证,它不应该做任何事情,因为服务返回 true 并且验证以false 启动,它会被正确保存。
引导程序:
UserWorkingTimeRegulation.create adminUser, workingTimeRegulation, new LocalDate(), new LocalDate().plusMonths(1)
【问题讨论】:
标签: grails grails-orm