Results 1 to 8 of 8

Thread: Thrown JpaSystemException instead of DuplicateKeyException

  1. #1
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Exclamation Thrown JpaSystemException instead of DuplicateKeyException

    Hi everyone.

    I'm in troubles with DataAccessException handling.

    When an uniquess key constraint is violated I got an JpaSystemException, and not a DuplicateKeyException!

    I found some thread talking about this problem, but no one help me to solve the problem. How can I map to a concrete org.springframework.dao exception?

    My persistence.xml file look like this:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    	<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>        
            <class>...</class>
            <class>...</class>
            ...
            <exclude-unlisted-classes />
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                <property name="hibernate.hbm2ddl.auto" value="update"/> 
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
                <property name="hibernate.show.sql" value="true"/>
                
                <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
       	    <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
       	    <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
       	    <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener" />
                <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener" />
       	    <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener" />
    	</properties>         
        </persistence-unit>    
        <persistence-unit name="persistenceUnitI24" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>...</class>
            <class>...</class>
            <exclude-unlisted-classes />
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
                <property name="hibernate.hbm2ddl.auto" value="validate"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
                <property name="hibernate.showsql" value="true"/>           
            </properties>
        </persistence-unit> 
    </persistence>
    And the configurations about mysql transaction manager:

    Code:
     <bean id="entityManagerFactory" 
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:dataSource-ref="dataSource"
              p:persistenceUnitName="persistenceUnit"
              p:jpaProperties="">
              <property name="jpaVendorAdapter">
    		<bean
    		  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    		      <property name="showSql" value="true" />
    		      <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    		      <property name="database" value="MYSQL"/>		      
    		 </bean>
    	   </property>
               <property name="jpaDialect">
    		 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
               </property>
        </bean>
        	  
        <bean id="transactionManager"
              class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory"/>
     
        <tx:annotation-driven transaction-manager="transactionManager" />
    I've omitted the configuration about sqlserver because I think that is not relevant. I'm not interested in translation error about this db.

    I've also included the bean:
    Code:
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    I'm using Spring 3.0.6.RELEASE and Hibernate 3.6.9.FINAL

    Thank you for any reply!
    Marco
    Last edited by mserioli; Jan 28th, 2012 at 06:21 AM.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    Hello Marco

    Could you post your complete error stack trace?

    According with the API of JpaSystemException

    JPA-specific subclass of UncategorizedDataAccessException, for JPA system errors that do not match any concrete org.springframework.dao exceptions.
    Now, I am confused about this

    You are using MySQL

    Code:
    <bean id="entityManagerFactory" 
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:dataSource-ref="dataSource"
              p:persistenceUnitName="persistenceUnit"
              p:jpaProperties="">
              <property name="jpaVendorAdapter">
    		<bean
    		  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    		      <property name="showSql" value="true" />
    		      <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    		      <property name="database" value="MYSQL"/>		      
    		 </bean>
    	   </property>
               <property name="jpaDialect">
    		 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
               </property>
        </bean>
    And later you said

    I've omitted the configuration about sqlserver because I think that is not relevant. I'm not interested in translation error about this db.
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Default

    Thank you for your reply!

    Now, I am confused about this
    I've omitted to say that in this project there are two DBs. One mysql db and one mssql. But I'm interested in mysql db!


    Let me explain more...

    Trying to to do the following simple junit test:
    Code:
    @Test(expected=DuplicateKeyException.class)
    public void createNewUbi(){
        Ubi ubi = new Ubi();
        ubi.setCode("A");
        ubiDao.save(ubi);       
    
        ubi = new Ubi();
        ubi.setCode("A");
    
        ubiDao.save(ubi);                   
    }
    I got from my log:

    Code:
    ERROR: org.hibernate.util.JDBCExceptionReporter - Duplicate entry 'A' for key 'code'
    Catching the exception of the second save call I noticed that I have:
    Code:
    JpaSystemException: cause: javax.persistence.PersistenceException
    PersistenceException: cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
    If I try to do an insertion from the webapp I got this Exception:
    Code:
    org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:311)
        org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
        it.cpmapave.pm.dao.jpa.GenericDaoJpa.save(GenericDaoJpa.java:29)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        $Proxy56.save(Unknown Source)
        it.cpmapave.pm.service.amministrazione.SettoreServiceImpl.saveSettore(SettoreServiceImpl.java:34)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        $Proxy57.saveSettore(Unknown Source)
        it.cpmapave.pm.controller.amministrazione.SettoreController.create(SettoreController.java:42)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        java.lang.reflect.Method.invoke(Method.java:597)
        org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
        org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
        org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      ...
    I've enabled mysql query logging and I got the following query:
    Code:
    insert into ubi (code, version) values ('A', 0)

    If I try to execute the query from mysql console I got:
    Code:
    ERROR 1062 (23000): Duplicate entry 'A' for key 'code'
    Searching in sql-error-codes.xml in package org.springframework.jdbc.support of spring-jdbc-3.0.6.RELEASE.jar i found that for mysql db the error code 1062 is configured in this way:

    Code:
    <property name="duplicateKeyCodes">
        <value>1062</value>
    </property>
    So I think that there is some Exception translating problem due to a misconfigured properties..
    Last edited by mserioli; Jan 23rd, 2012 at 12:42 AM.

  4. #4
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Default

    I've found that when maven compile the project I get this WARNING:

    advice defined in org.springframework.orm.jpa.aspectj.JpaExceptionTr anslatorAspect has not been applied

    Could it be a cause?

  5. #5
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Default

    the cause is not that. nothing has changed..

  6. #6
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Exclamation

    no one has ever had this problem?

  7. #7
    Join Date
    Mar 2005
    Location
    Copenhagen, Denmark
    Posts
    17

    Default

    I have the same problem - did you find a solution?

  8. #8
    Join Date
    May 2010
    Location
    Angolo Terme, BS, Italy
    Posts
    57

    Default

    No.. I'm sorry

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
  •