2013-01-11 52 views
4

我有一個奇怪的行爲差異,我想了解。我在實體類中有Bean驗證註釋。我也有一些捕獲它們的代碼,並將它們格式化爲JSON作爲REST接口 - 但我認爲這不會影響任何內容。我使用Spring和JPA Hibernate。Postgres訴HSQL ConstrainViolationException在春季包裝

當我設置HSQL作爲我的數據庫驅動程序,我可以一個EntityManager #persist調用後直接趕ConstrainViolationException。但是,當我將JDBC驅動程序切換到Postgres時,#persist會拋出一個由RollbackException引發的TransactionSystemException,最後在RollbackException中我可以得到ConstraintViolationException。代碼是相同的,我只能切換驅動程序。 #persist方法是@Transactional。

現在我想我可以通過爲TransactionSystemExeption添加一個catch並獲取兩次原因來處理ConstraintViolationException來解決這個問題,但對我來說似乎很奇怪,我需要兩個相同的代碼的catch子句,具體取決於驅動配置。任何想法或解釋?

設置很簡單。任何JPA實體在某些字段上具有@NotNull bean約束。然後堅持它是一個事務性的方法。使用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 
+0

我不能現在就調試休眠源,但我的猜測是,與HSQL,Hibernate是我的過程中使用的代碼來執行豆驗證之前插入/提交發生PostgreSQL的同時不論是在插入或承諾做。因此,對於HSQL,它只拋出ConstraintViolationException,而對於PostgreSQL,它是一個提交異常,Hibernate將其封裝爲RollbackException,然後Spring將其作爲TransactionSystemException包裝。這只是一個猜測。 –

+0

向我們展示代碼,實體及其映射,以及例外的堆棧跟蹤。我們沒有任何水晶球。 –

+0

好的@JBNizet我添加了堆棧跟蹤。 –

回答

2

不幸的是,休眠不處理在驅動程序無關的方式驗證。因此,您將不得不根據數據庫以不同的方式捕獲這些異常。不要以爲您可以以獨立於數據庫的方式編寫您的實體異常和驗證處理代碼。