ConstraintViolationException VS DataIntegrityViolationException [英] ConstraintViolationException VS DataIntegrityViolationException
问题描述
我正在编写一个Spring应用程序,看来当我遇到数据库错误时,有时会抛出Hibernate的ConstraintViolationException
而有时会抛出Spring的DataIntegrityViolationException
.是否有可能会抛出一个原因而不是另一个原因?他们的意思不同吗?
I am writing a Spring app, and it seems that when I run into database errors, sometimes Hibernate's ConstraintViolationException
is thrown and sometimes Spring's DataIntegrityViolationException
is thrown. Is there a reason one might be thrown and not the other? Do they mean different things?
推荐答案
如果您通过HibernateTemplate
执行Hibernate调用,那么Spring会将所有Hibernate异常(例如ConstraintViolationException
)转换为Spring异常层次结构(例如HibernateTemplate
,那么本地的Hibernate异常将通过.两者都是RuntimeExceptions
,因此不需要显式的异常处理,因此要看到哪一个并不总是很明显.
If you execute Hibernate calls via the HibernateTemplate
, then Spring will translate any Hibernate exceptions (e.g. ConstraintViolationException
) into the Spring exception hierarchy (e.g. DataIntegrityViolationException
). If you don't use HibernateTemplate
, then the native Hibernate exceptions will go through. Both are RuntimeExceptions
, so don't need explicit exception handling, so it's not always obvious which one you're going to see.
这篇关于ConstraintViolationException VS DataIntegrityViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!