Results 1 to 2 of 2

Thread: Spring Exception Translation with JPA

  1. #1
    Join Date
    Sep 2008
    Posts
    22

    Default Spring Exception Translation with JPA

    Hi. We have implemented persistence using Hibernate as the vendor but using jpa annotations. Now we need to do transaction exception translation so that we can handle various persistence exceptions gracefully. The section in the docs on exception translation was not too clear, but I tried to follow it as well as I could. In particular, we need to be able to catch a constraint violated exception, so that we can report that error.

    AccountRepository.java
    Code:
    @Repository
    public class AccountRepository {
    
    @PersistenceContext(unitName = "paymentsPersistenceUnit")
    public void setEntityManager( EntityManager entityManager ) {
     this.entityManager = entityManager;
    }
    
    ....
    public void persistAccount(Account account) {
    
     EntityManager.persist(account)
    }
    ....
    
    }

    AccountApp.java
    Code:
    ...
    public void createAccount() {
    Account account = new Account();
    ...
    try {
       AccountRepository.persistAccount(account);
    catch(org.springframework.dao.DataIntegrityViolationException dive) {
       reportConstraintViolation()
    }
    ...
    }
    ...
    app-context.xml
    Code:
    <import resource="reg-context.xml"/>
    
    
    <bean id ="paymentsDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ....
    </bean>
    </beans>

    reg-context.xml
    Code:
    <aop spring-configured/>
    <bean id="accountRepository" autowire="byName" class="com.qpass.payments.model.AccountRepositoryImpl">
         <property name="paymentsDomainEntityValidator" ref="paymentsDomainEntityValidator" />
    </bean>
    
    <bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <bean id="paymentsEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="persistenceXmlLocation" value="some location"/>
     <property name="persistenceUnitName" value="paymentsPersistenceUnit"/>
     <property name="dataSource" ref="paymentsDataSource" />
     <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
         <property name="showSql" value="true" />
         <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
       </bean>
     </property>
    </bean>
    
    <bean id="paymentsTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
     <property name="entityManagerFactory" ref="paymentsEntityManagerFactory"/>
    </bean>
    
    
    <bean id="paymentsTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
     <property name="transactionManager" ref="paymentsTransactionManager"/>
     <property name="transactionAttributeSource">
       <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
     </property>
    </bean>
    
    
    <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
     <property name="transactionInterceptor" ref="paymentsTransactionInterceptor"/>
     <property name="classFilter">
       <bean class="org.springframework.aop.aspectj.TypePatternClassFilter">
         ...
       </bean>
     </property>
    </bean>
    </beans>
    It looked from the spring docs like I just had to set add the bean org.springframework.dao.annotation.PersistenceExce ptionTranslationPostProcessor to my context and it would automagically translate exceptions for me (not that that makes sense). Anyway, the way that I ghave it wired now, it isn't throwing any DataIntegrityViolationException. It is either throwing spring UnexpectedRollbackExceptions or org.hibernate.exception.ConstraintViolationExcepti ons. Any help on wiring this up correctly?

    There might be a few typos in the above code. I had to censor pieces of it. This forum also got a bit grouchy about some of my code--it seemed to think I was trying to post urls, so I had to excise a few bits. If you need to know more, please ask.

  2. #2

    Default

    I have the exact same issues -- how did you get past this?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •