Announcement Announcement Module
Collapse
No announcement yet.
Exception Translation Problem - Spring,JPA and Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception Translation Problem - Spring,JPA and Hibernate

    Hi,
    I am having an application running on Spring with JPA using Hibernate.I am having some doubts regarding the springs automatic exception translation policy.
    First of all, all my data access calls are through JPATemplate. What I read from some of the docs is, if you have wrapped all of your calls inside Spring Dao template class,it will automatically do the exception translation and we are not required to register the bean post processor PersistenceExceptionTranslationPostProcessor in the context file.Is my understanding correct?. So I have tried running my application with and without registering the post processor and didnt find any difference.
    But I observed some inconsistent behaviour in the exception translation for my application.When I tried creating an error in my application,by violating a not null constraint I got the following error

    Code:
    Exception in thread "main" org.springframework.orm.jpa.JpaSystemException: org.hibernate.PropertyValueException: not-null property references a null or transient value: 
    nested exception is javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: 
    
    Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.....................
    
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
    
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:219)
    
    at org.springframework.orm.jpa.JpaTemplate$5.doInJpa(JpaTemplate.java:263)
    I found that JpaSystemException is a DataAcessException subclass for unclassified errors.Is this the exception I should get on null constraint violation when exception translation have been plugged in?

    At another point I tried with a refrential constraint violation and I got plain hibernate exceptions from my application.Stacktrace is shown below

    Code:
    2007-12-07 06:16:06 ERROR pool-4-thread-1 [] logExceptions(JDBCExceptionReporter:78) - ORA-02291: integrity constraint (XML_USER.FEED_OUTPUT_SUMM_XREF_FK_02) violated - parent key not found
    
     
    
    2007-12-07 06:16:06 ERROR pool-4-thread-1 [] logExceptions(JDBCExceptionReporter:78) - ORA-02291: integrity constraint (XML_USER.FEED_OUTPUT_SUMM_XREF_FK_02) violated - parent key not found
    
     
    
    2007-12-07 06:16:06 ERROR pool-4-thread-1 [] performExecutions(AbstractFlushingEventListener:301) - Could not synchronize database state with session
    
    org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    
                at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    
                at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    
                at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    
                at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    
                at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    
                at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    
                at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    
                at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    
                at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    
                at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    
                at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
    
                at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)
    
                at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
    
                at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
    
                at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
    
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
    
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
    
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    
                at $Proxy42.create(Unknown Source)
    
         .........
    
                at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    
                at java.util.concurrent.FutureTask.run(Unknown Source)
    
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    
                at java.lang.Thread.run(Unknown Source)
    
    Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (XML_USER.FEED_OUTPUT_SUMM_XREF_FK_02) violated - parent key not found
    
     
    
                at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
    
                at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
    
                at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    
                at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    
                ... 24 more
    
    org.springframework.transaction.UnexpectedRollbackException: JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation); nested exception is javax.persistence.RollbackException: Error while commiting the transaction
    
    Caused by: javax.persistence.RollbackException: Error while commiting the transaction
    
                at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
    
                at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)
    
                at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
    
                at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
    
                at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
    
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
    
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
    
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    
                at $Proxy42.create(Unknown Source)
    
                at .......com......
    
                at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    
                at java.util.concurrent.FutureTask.run(Unknown Source)
    
                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    
                at java.lang.Thread.run(Unknown Source)
    
    Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    
                at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    
                at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    
                at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    
                at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    
                at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    
                at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    
                at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    
                at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    
                at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    
                at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    
                at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
    
                ... 16 more
    I think my exception translation is not working correctly.What could be the reason.Waiting for some response......

    I have tried this both enabling/disabling the post processor.

  • #2
    somebody please repsond to this..... :-(

    Comment


    • #3
      It all depends on your configuration of the EntityManagerFactoryBean. If you specify an JpaDialect that will convert exceptions, if you don't the DefaultJpaDialect is used. That can only handle generic JpaExceptions not exceptions thrown by specific implementation (as what is happening in this case).

      It could be argued that this is a Hibernate issue, you use it as a JPA implementation and not as Hibernate so I guess it should throw JPA exceptions...

      However register a HibernateJpaDialect and exceptions should be converted.

      Comment


      • #4
        My context file is autowired by name and the entitymanager configuration is


        Code:
        <bean id="jpaDialect"
        		class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        
        	<!-- uses the persistence unit defined in the META-INF/persistence.xml JPA configuration file -->
        	<bean id="entityManagerFactory"
        		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        
        		<!--<property name="dataSource" ref="dataSource" /> -->
        		<property name="persistenceXmlLocation">
        			<value>classpath:/META-INF/persistence.xml</value>
        		</property>
        		<property name="jpaVendorAdapter">
        			<bean
        				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        				<property name="showSql" value="true" />
        				<property name="databasePlatform"
        					value="org.hibernate.dialect.Oracle9Dialect" />
        			</bean>
        		</property>
        	</bean>
        So I think I have done that part correctly?

        But what about that constraint violation exception from hibernate.Is there no
        Spring DA exceptions defined for them?

        Comment


        • #5
          But what about that constraint violation exception from hibernate.Is there no
          Spring DA exceptions defined for them?
          Exception handling/conversion depends on the implementation, what use would it be to create on gigantic exception convertion which could handle everything including exceptions from the kitchen sink (so to speak ).

          Why have you specified the persistenceXmlLocation the one specified should be the default. It could be that you have properties in there which interfere with things from the JpaDialect.

          Comment


          • #6
            These are the properties I have defined in my persistence.xml

            Code:
            <properties>
            			<property name="hibernate.dialect"
            				value="org.hibernate.dialect.Oracle9Dialect" />
            			<property name="hibernate.show_sql" value="true" />
            			<property name="hibernate.format_sql" value="true" />
            			<property name="hibernate.cglib.use_reflection_optimizer"
            				value="true" />
            			<property name="hibernate.cache.provider_class"
            				value="org.hibernate.cache.EhCacheProvider" />
            			<property name="hibernate.cache.use_query_cache"
            				value="true" />
            			<property name="hibernate.current_session_context_class"
            				value="thread" />
            			<!--
            				<property name="hibernate.transaction.factory_class"
            				value="org.hibernate.transaction.JDBCTransactionFactory" />
            			-->
            		</properties>
            Does any of them can cause a problem?

            Comment


            • #7
              Well there is some stuff which isn't necessary and which might cause troubles.

              Code:
              <properties>
              			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
              			<property name="hibernate.show_sql" value="true" />
              			<property name="hibernate.format_sql" value="true" />
              			<property name="hibernate.cglib.use_reflection_optimizer" value="true" />
              			<property name="hibernate.cache.provider_class"
              				value="org.hibernate.cache.EhCacheProvider" />
              			<property name="hibernate.cache.use_query_cache" value="true" />
              			<property name="hibernate.current_session_context_class" value="thread" />
              			<!--
              				<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
              			-->
              </properties>
              The stuff highlighted in red is duplicate or might cause issues. However that doesn't explain the issues you have (AFAIK).

              In your configuration you don't need the jpaDialect, it is automatically set by the JpaVendorAdapter.
              Code:
              <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
              
              <!-- uses the persistence unit defined in the META-INF/persistence.xml JPA configuration file -->
              <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              
                <!--<property name="dataSource" ref="dataSource" /> -->
                <property name="persistenceXmlLocation">
                  <value>classpath:/META-INF/persistence.xml</value>
                </property>
                <property name="jpaVendorAdapter">
                  <bean
                    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true" />
                    <property name="databasePlatform"
                      value="org.hibernate.dialect.Oracle9Dialect" />
                  </bean>
                </property>
              </bean>
              Next to that I wouldn't rely on automatic beanwiring by name, but that might just be me.

              Comment


              • #8
                I have the same issue here, how did this end up?

                Comment

                Working...
                X