我有一个奇怪的行为差异,我想了解一下。我在实体类中有 Bean 验证注释。我还有一些代码可以捕获它们,并将它们格式化为 REST 接口(interface)的 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
最佳答案
不幸的是,hibernate 不会以与驱动程序无关的方式处理验证。因此,您将不得不根据数据库以不同方式捕获这些异常。不要假设您可以以独立于数据库的方式编写实体异常和验证处理代码。