2017-03-09 8 views
0

My 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キャッチしようとした理由:代わりに、最初のcatchステートメントに行こうと、私の驚きに

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.") 
} 

を、それは2番目のものに行き、印刷されました:

class org.springframework.orm.hibernate4.HibernateSystemException 

何がありますか? IdentifierGenerationExceptionはすべてHibernateSystemExceptionで処理されると想定するのが安全です。他のExceptionがあり、HibernateSystemExceptionの下に「」と分類されている場合はどうなりますか?

これは私のtry-catch文はのように見えた今である:私はGrailsの2.4.4に休止状態4を使用してい

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.") 
} 

+0

投稿の例外の完全なスタックトレースを追加してください。 Springの例外処理では、 'DataAccessException'を使用する必要があります。 – Chaitanya

答えて

0

IdentifierGenerationExceptionは、HibernateSystemExceptionのネストされた例外です。したがって、IDなしのオブジェクトだけを捕捉したい場合は、HibernateSystemExceptionを捕まえるべきではありません。IdentifierGenerationException以外でもHibernateSystemExceptionが原因である可能性があります。

例外の原因を調べて、ネストされた例外がinstanceof IdentifierGenerationExceptionであるかどうかを確認する必要があります。または、オブジェクトIDがnullかどうかをチェックします。

関連する問題