Postgres v.HSQL ConstrainViolationException在Spring中包装

时间:2013-01-11 17:40:50

标签: spring hibernate jpa hsqldb

我有一种奇怪的行为差异,我想了解。我在实体类中有Bean Validation注释。我也有一些捕获它们的代码,它们将它们格式化为REST接口的JSON - 但我认为这不会影响任何东西。我正在使用Spring和JPA Hibernate。

当我将HSQL设置为我的数据库驱动程序时,我可以在EntityManager #persist调用之后直接捕获ConstrainViolationException。但是,当我将JDBC驱动程序切换到Postgres时#persist将抛出由RollbackException引起的TransactionSystemException,最后在RollbackException中我可以得到ConstraintViolationException。代码是相同的,我只切换驱动程序。 #persist方法是@Transactional。

现在我想我可以通过为TransactionSystemExeption添加一个catch并获取原因两次来处理ConstraintViolationException来解决这个问题,但是对我来说似乎很奇怪,我需要两个catch子句来代码只依赖于驱动程序组态。有什么想法或解释吗?

设置很简单。在某些字段上具有@NotNull bean约束的任何JPA实体。然后将其保存在事务性方法中。使用HSQL,您将得到如下例外:

javax.validation.ConstraintViolationException: Validation failed for classes [MyEntity] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='not a well-formed email address', propertyPath=emailAddress, rootBeanClass=class ..., messageTemplate='{org.hibernate.validator.constraints.Email.message}'}
    ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=password, rootBeanClass=class ..., messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:161)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
    at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:160)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:837)
    at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at $Proxy49.persist(Unknown Source)

使用PostgreSQL时,您将获得此异常。不要更改任何代码,只需将驱动程序和方言更改为PostgreSQL并捕获异常。

    org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$afterReturning$org_springframework_transaction_aspectj_AbstractTransactionAspect$3$2a73e96c(AbstractTransactionAspect.aj:78)
    at EntityService.persist(EntityService.java:82)
    ...
Caused by: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
    ... 47 more
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [MyEntity] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='not a well-formed email address', propertyPath=emailAddress, rootBeanClass=class MyEntity, messageTemplate='{org.hibernate.validator.constraints.Email.message}'}
    ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=password, rootBeanClass=class MyEntity, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:161)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
    at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:178)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
    ... 48 more

1 个答案:

答案 0 :(得分:2)

不幸的是,hibernate不以驱动程序无关的方式处理验证。因此,您必须根据数据库以不同方式捕获这些异常。不要假设您可以以独立于数据库的方式编写实体异常和验证处理代码。