我的Hibernate对象需要在保存之前手动分配ID。但我忘了分配它,所以这个错误日志出现了:
ERROR errors.GrailsExceptionResolver - IdentifierGenerationException occurred when processing request: [POST] /hrims/hapum/maintenance/department/save
Stacktrace follows:
Message: ids for this class must be manually assigned before calling save(): ph.gov.nhmfc.hapum.Department
Line | Method
->> 24 | save in ph.gov.nhmfc.DomainService$$EQDMPY5t
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 191 | save in ph.gov.nhmfc.hapum.DepartmentService$$EQDM563D
| 85 | save . . . . . . in ph.gov.nhmfc.hapum.maintenance.DepartmentController
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter . . . . in grails.plugin.cache.web.filter.AbstractFilter
| 449 | executeChain in org.apache.shiro.web.servlet.AbstractShiroFilter
| 365 | call . . . . . . in org.apache.shiro.web.servlet.AbstractShiroFilter$1
| 90 | doCall in org.apache.shiro.subject.support.SubjectCallable
| 83 | call . . . . . . in ''
| 383 | execute in org.apache.shiro.subject.support.DelegatingSubject
| 362 | doFilterInternal in org.apache.shiro.web.servlet.AbstractShiroFilter
| 125 | doFilter in org.apache.shiro.web.servlet.OncePerRequestFilter
| 1145 | runWorker . . . in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run . . . . . . in java.lang.Thread
这就是为什么我试图抓住IdentifierGenerationException
:
try {
hibernate.save(insert: true)
}
catch(IdentifierGenerationException e) {
errors.push("An identifier should be assigned for this record before saving.")
}
catch(Exception e) {
println(e.getClass())
errors.push("An error has occured.")
}
令我惊讶的是,它没有进入第一个catch
声明,而是转到第二个并打印出来:
class org.springframework.orm.hibernate4.HibernateSystemException
是什么给出的?可以安全地假设IdentifierGenerationException
将处理所有HibernateSystemException
。如果在Exception
下有"分类" 的其他HibernateSystemException
怎么办?
现在我的try-catch
声明如下:
try {
hibernate.save(insert: true)
}
catch(IdentifierGenerationException e) {
errors.push("An identifier should be assigned for this record before saving.")
}
catch(HibernateSystemException e) {
errors.push("An identifier should be assigned for this record before saving.")
}
catch(Exception e) {
errors.push("An unidentifieable error has occured.")
}
我在Grails 2.4.4上使用Hibernate 4。
答案 0 :(得分:0)
在您的情况下,似乎IdentifierGenerationException
是HibernateSystemException
的嵌套例外。因此,如果您只想捕获没有id的对象,则不应尝试捕获HibernateSystemException
,因为HibernateSystemException
也可能由IdentifierGenerationException
以外的其他原因引起。
您应该检查异常的原因,然后检查嵌套异常是否为IdentifierGenerationException
的实例。或者检查对象id是否为null。