Announcement Announcement Module
Collapse
No announcement yet.
AssertionFailure after a duplicate record exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AssertionFailure after a duplicate record exception

    I have an object that define below. The object is able to save to the database if the "name" field is unique. However, when I set the name field (which has a unique constraint) to a duplicate value, I am getting two exceptions. The first one complaints about a duplicate entry which is expected. But the second exception occurs just after is a AssertionFailure from Hibernate. But if I change the key generator to "assigned" and manually assign it a unique key, the same code work perfectly with the appropiate exception propagated to my code. I like to have the key field automatically assigned by the database. Has anyone encounter this problem and have a solution for this. Below is the complete stack trace and more details.

    Thanks for you help.
    Tat

    Hibernate version:
    3.0.5 and 3.1RC1

    Mapping documents:


    Code:
    <hibernate-mapping
    >
        <class
            name="com.lead.model.Lead"
            table="lead"
        >
    
            <id
                name="key"
                column="lead_id"
                type="long"
                unsaved-value="null"
            >
                <generator class="native">
                </generator>
            </id>
    
            <property
                name="name"
                type="java.lang.String"
                update="true"
                insert="true"
                column="name"
                not-null="false"
                unique="true"
            />
    
        </class>
    
    </hibernate-mapping>
    Code between sessionFactory.openSession() and session.close():

    Full stack trace of any exception that occurs:
    Code:
    2005.10.21 23&#58;37&#58;35&#58;778 main&#58;DEBUG&#58;StringType binding 'foo' to parameter&#58; 1
    2005.10.21 23&#58;37&#58;35&#58;823 main&#58;DEBUG&#58;AbstractBatcher about to close PreparedStatement &#40;open PreparedStatements&#58; 1, globally&#58; 1&#41;
    2005.10.21 23&#58;37&#58;35&#58;824 main&#58;DEBUG&#58;AbstractBatcher closing statement
    2005.10.21 23&#58;37&#58;35&#58;836 main&#58;DEBUG&#58;JDBCExceptionReporter could not insert&#58; &#91;com.lead.model.Lead&#93; &#91;insert into lead &#40;name&#41; values &#40;?&#41;&#93;
    &#91;color=red&#93;&#91;b&#93;java.sql.SQLException&#58; Duplicate entry 'foo' for key 2&#91;/b&#93;&#91;/color&#93;	at com.mysql.jdbc.MysqlIO.checkErrorPacket&#40;MysqlIO.java&#58;2921&#41;
    	at com.mysql.jdbc.MysqlIO.sendCommand&#40;MysqlIO.java&#58;1570&#41;
    	at com.mysql.jdbc.ServerPreparedStatement.serverExecute&#40;ServerPreparedStatement.java&#58;1085&#41;
    	at com.mysql.jdbc.ServerPreparedStatement.executeInternal&#40;ServerPreparedStatement.java&#58;670&#41;
    	at com.mysql.jdbc.PreparedStatement.executeUpdate&#40;PreparedStatement.java&#58;1159&#41;
    	at com.mysql.jdbc.PreparedStatement.executeUpdate&#40;PreparedStatement.java&#58;1076&#41;
    	at com.mysql.jdbc.PreparedStatement.executeUpdate&#40;PreparedStatement.java&#58;1061&#41;
    	at org.hibernate.persister.entity.BasicEntityPersister.insert&#40;BasicEntityPersister.java&#58;1759&#41;
    	at org.hibernate.persister.entity.BasicEntityPersister.insert&#40;BasicEntityPersister.java&#58;2178&#41;
    	at org.hibernate.action.EntityIdentityInsertAction.execute&#40;EntityIdentityInsertAction.java&#58;34&#41;
    	at org.hibernate.engine.ActionQueue.execute&#40;ActionQueue.java&#58;239&#41;
    	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate&#40;AbstractSaveEventListener.java&#58;240&#41;
    	at org.hibernate.event.def.AbstractSaveEventListener.performSave&#40;AbstractSaveEventListener.java&#58;160&#41;
    	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId&#40;AbstractSaveEventListener.java&#58;95&#41;
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId&#40;DefaultSaveOrUpdateEventListener.java&#58;184&#41;
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient&#40;DefaultSaveOrUpdateEventListener.java&#58;173&#41;
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate&#40;DefaultSaveOrUpdateEventListener.java&#58;96&#41;
    	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate&#40;DefaultSaveOrUpdateEventListener.java&#58;69&#41;
    	at org.hibernate.impl.SessionImpl.saveOrUpdate&#40;SessionImpl.java&#58;468&#41;
    	at org.hibernate.impl.SessionImpl.saveOrUpdate&#40;SessionImpl.java&#58;463&#41;
    	at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate&#40;HibernateTemplate.java&#58;673&#41;
    	at org.springframework.orm.hibernate3.HibernateTemplate.execute&#40;HibernateTemplate.java&#58;358&#41;
    	at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate&#40;HibernateTemplate.java&#58;670&#41;
    	at com.lead.dao.hibernate.LeadDAOHibernate.saveLead&#40;LeadDAOHibernate.java&#58;52&#41;
    	at com.lead.service.impl.LeadManagerImpl.saveLead&#40;LeadManagerImpl.java&#58;44&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection&#40;AopUtils.java&#58;292&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint&#40;ReflectiveMethodInvocation.java&#58;155&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;122&#41;
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke&#40;TransactionInterceptor.java&#58;96&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;144&#41;
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke&#40;JdkDynamicAopProxy.java&#58;174&#41;
    	at $Proxy4.saveLead&#40;Unknown Source&#41;
    	at com.lead.service.LeadExistsExceptionTest.testAddExistingLead&#40;LeadExistsExceptionTest.java&#58;55&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at junit.framework.TestCase.runTest&#40;TestCase.java&#58;154&#41;
    	at org.jmock.core.VerifyingTestCase.runBare&#40;Unknown Source&#41;
    	at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
    	at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
    	at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
    	at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
    	at junit.framework.TestSuite.runTest&#40;TestSuite.java&#58;208&#41;
    	at junit.framework.TestSuite.run&#40;TestSuite.java&#58;203&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;478&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;344&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;196&#41;
    2005.10.21 23&#58;37&#58;35&#58;841 main&#58;WARN&#58;JDBCExceptionReporter SQL Error&#58; 1062, SQLState&#58; 23000
    2005.10.21 23&#58;37&#58;35&#58;841 main&#58;ERROR&#58;JDBCExceptionReporter Duplicate entry 'foo' for key 2
    2005.10.21 23&#58;37&#58;35&#58;867 main&#58;DEBUG&#58;JDBCTransaction commit
    2005.10.21 23&#58;37&#58;35&#58;868 main&#58;DEBUG&#58;SessionImpl automatically flushing session
    2005.10.21 23&#58;37&#58;35&#58;869 main&#58;DEBUG&#58;AbstractFlushingEventListener flushing session
    2005.10.21 23&#58;37&#58;35&#58;871 main&#58;DEBUG&#58;AbstractFlushingEventListener processing flush-time cascades
    2005.10.21 23&#58;37&#58;35&#58;872 main&#58;DEBUG&#58;AbstractFlushingEventListener dirty checking collections
    2005.10.21 23&#58;37&#58;35&#58;873 main&#58;DEBUG&#58;AbstractFlushingEventListener Flushing entities and processing referenced collections
    2005.10.21 23&#58;37&#58;35&#58;873 main&#58;ERROR&#58;&#91;color=red&#93;&#91;b&#93;AssertionFailure an assertion failure occured &#40;this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session&#41;
    org.hibernate.AssertionFailure&#58; null id in entry &#40;don't flush the Session after an exception occurs&#41;&#91;/b&#93;&#91;/color&#93;	at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId&#40;DefaultFlushEntityEventListener.java&#58;49&#41;
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity&#40;DefaultFlushEntityEventListener.java&#58;82&#41;
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities&#40;AbstractFlushingEventListener.java&#58;190&#41;
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions&#40;AbstractFlushingEventListener.java&#58;70&#41;
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush&#40;DefaultFlushEventListener.java&#58;26&#41;
    	at org.hibernate.impl.SessionImpl.flush&#40;SessionImpl.java&#58;730&#41;
    	at org.hibernate.impl.SessionImpl.managedFlush&#40;SessionImpl.java&#58;324&#41;
    	at org.hibernate.transaction.JDBCTransaction.commit&#40;JDBCTransaction.java&#58;86&#41;
    	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit&#40;HibernateTransactionManager.java&#58;490&#41;
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit&#40;AbstractPlatformTransactionManager.java&#58;495&#41;
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit&#40;AbstractPlatformTransactionManager.java&#58;468&#41;
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.doCloseTransactionAfterThrowing&#40;TransactionAspectSupport.java&#58;294&#41;
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke&#40;TransactionInterceptor.java&#58;100&#41;
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed&#40;ReflectiveMethodInvocation.java&#58;144&#41;
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke&#40;JdkDynamicAopProxy.java&#58;174&#41;
    	at $Proxy4.saveLead&#40;Unknown Source&#41;
    	at com.lead.service.LeadExistsExceptionTest.testAddExistingLead&#40;LeadExistsExceptionTest.java&#58;55&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	at junit.framework.TestCase.runTest&#40;TestCase.java&#58;154&#41;
    	at org.jmock.core.VerifyingTestCase.runBare&#40;Unknown Source&#41;
    	at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
    	at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
    	at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
    	at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
    	at junit.framework.TestSuite.runTest&#40;TestSuite.java&#58;208&#41;
    	at junit.framework.TestSuite.run&#40;TestSuite.java&#58;203&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;478&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;344&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;196&#41;
    2005.10.21 23&#58;37&#58;35&#58;879 main&#58;DEBUG&#58;JDBCTransaction rollback
    2005.10.21 23&#58;37&#58;35&#58;879 main&#58;DEBUG&#58;JDBCContext before transaction completion
    2005.10.21 23&#58;37&#58;35&#58;879 main&#58;DEBUG&#58;SessionImpl before transaction completion
    2005.10.21 23&#58;37&#58;35&#58;880 main&#58;DEBUG&#58;JDBCTransaction re-enabling autocommit
    2005.10.21 23&#58;37&#58;35&#58;881 main&#58;DEBUG&#58;JDBCTransaction rolled back JDBC Connection
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;JDBCContext after transaction completion
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;SessionImpl after transaction completion
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;SessionImpl closing session
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;ConnectionManager closing JDBC connection &#91; &#40;open PreparedStatements&#58; 0, globally&#58; 0&#41; &#40;open ResultSets&#58; 0, globally&#58; 0&#41;&#93;
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;JDBCContext after transaction completion
    2005.10.21 23&#58;37&#58;35&#58;882 main&#58;DEBUG&#58;SessionImpl after transaction completion
    Name and version of the database you are using:
    MySQL 4.1

  • #2
    As the unique constrains fails the id can't be generated and that's why the second error (hb has to rebind the id to the actual java object).

    Comment


    • #3
      AssertionFailure after a duplicate record exception

      After some digging, I finally figured out what my problem was - just lack of knowledge of how spring's declaritive transaction works. After adding -DataIntegrityViolationException as shown below, everything works as expected.

      Code:
          <bean id="txProxyTemplate" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              <property name="transactionManager" ref="transactionManager"/>
              <property name="transactionAttributes">
                  <props>
                      <prop key="save*">PROPAGATION_REQUIRED,-DataIntegrityViolationException</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                  </props>
              </property>
          </bean>

      Comment

      Working...
      X