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