Results 1 to 10 of 25

Thread: Integration test causing a duplicate entry exception on insert

Hybrid View

  1. #1
    Join Date
    Mar 2008
    Posts
    258

    Default Integration test causing a duplicate entry exception on insert

    I'm running an integration test and get an exception for a duplicate entry exception on insert.

    The integration test has been generated by Roo with the database reverse engineering.

    -------------------------------------------------------------------------------
    Test set: com.learnintouch.lms.core.domain.ElearningTeacherI ntegrationTest
    -------------------------------------------------------------------------------
    Tests run: 9, Failures: 0, Errors: 8, Skipped: 0, Time elapsed: 1.085 sec <<< FAILURE!
    testFlush(com.learnintouch.lms.core.domain.Elearni ngTeacherIntegrationTest) Time elapsed: 0.102 sec <<< ERROR!
    org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.lms.core.domain.ElearningTeacher]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.learnintouch.lms.core.domain.ElearningTeacher]
    at org.springframework.orm.jpa.EntityManagerFactoryUt ils.convertJpaAccessExceptionIfPossible(EntityMana gerFactoryUtils.java:326)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTr anslatorAspect.ajc$afterThrowing$org_springframewo rk_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$ 18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.learnintouch.lms.core.domain.ElearningTeacher_ Roo_Jpa_ActiveRecord.ajc$interMethod$com_learninto uch_lms_core_domain_ElearningTeacher_Roo_Jpa_Activ eRecord$com_learnintouch_lms_core_domain_Elearning Teacher$persist(ElearningTeacher_Roo_Jpa_ActiveRec ord.aj:43)
    at com.learnintouch.lms.core.domain.ElearningTeacher. persist(ElearningTeacher.java:1)
    at com.learnintouch.lms.core.domain.ElearningTeacher_ Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_ learnintouch_lms_core_domain_ElearningTeacher_Roo_ Jpa_ActiveRecord$com_learnintouch_lms_core_domain_ ElearningTeacher$persist(ElearningTeacher_Roo_Jpa_ ActiveRecord.aj)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_l earnintouch_lms_core_domain_ElearningTeacherDataOn Demand_Roo_DataOnDemand$com_learnintouch_lms_core_ domain_ElearningTeacherDataOnDemand$init(Elearning TeacherDataOnDemand_Roo_DataOnDemand.aj:81)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand.init(ElearningTeacherDataOnDemand.java :1)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand_Roo_DataOnDemand.ajc$interMethodDispat ch1$com_learnintouch_lms_core_domain_ElearningTeac herDataOnDemand_Roo_DataOnDemand$com_learnintouch_ lms_core_domain_ElearningTeacherDataOnDemand$init( ElearningTeacherDataOnDemand_Roo_DataOnDemand.aj)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand_Roo_DataOnDemand.ajc$interMethod$com_l earnintouch_lms_core_domain_ElearningTeacherDataOn Demand_Roo_DataOnDemand$com_learnintouch_lms_core_ domain_ElearningTeacherDataOnDemand$getRandomElear ningTeacher(ElearningTeacherDataOnDemand_Roo_DataO nDemand.aj:56)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand.getRandomElearningTeacher(ElearningTea cherDataOnDemand.java:1)
    at com.learnintouch.lms.core.domain.ElearningTeacherD ataOnDemand_Roo_DataOnDemand.ajc$interMethodDispat ch1$com_learnintouch_lms_core_domain_ElearningTeac herDataOnDemand_Roo_DataOnDemand$com_learnintouch_ lms_core_domain_ElearningTeacherDataOnDemand$getRa ndomElearningTeacher(ElearningTeacherDataOnDemand_ Roo_DataOnDemand.aj)
    at com.learnintouch.lms.core.domain.ElearningTeacherI ntegrationTest_Roo_IntegrationTest.ajc$interMethod $com_learnintouch_lms_core_domain_ElearningTeacher IntegrationTest_Roo_IntegrationTest$com_learnintou ch_lms_core_domain_ElearningTeacherIntegrationTest $testFlush(ElearningTeacherIntegrationTest_Roo_Int egrationTest.aj:71)
    at com.learnintouch.lms.core.domain.ElearningTeacherI ntegrationTest.testFlush(ElearningTeacherIntegrati onTest.java:1)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements .RunBeforeTestMethodCallbacks.evaluate(RunBeforeTe stMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements .RunAfterTestMethodCallbacks.evaluate(RunAfterTest MethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements .SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:222)
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:300)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 )
    at org.apache.maven.surefire.junit4.JUnit4TestSet.exe cute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.ex ecuteTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.in voke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.inv okeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$P roviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.i nvokeProvider(SurefireStarter.java:172)
    at org.apache.maven.surefire.booter.SurefireStarter.r unSuitesInProcessWhenForked(SurefireStarter.java:1 04)
    at org.apache.maven.surefire.booter.ForkedBooter.main (ForkedBooter.java:70)

    ...

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Duplicate entry '3527' for key 'user_id_2'
    at sun.reflect.GeneratedConstructorAccessor47.newInst ance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1039)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:20 02)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(P reparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2345)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2330)
    at org.apache.commons.dbcp.DelegatingPreparedStatemen t.executeUpdate(DelegatingPreparedStatement.java:1 05)
    at org.apache.commons.dbcp.DelegatingPreparedStatemen t.executeUpdate(DelegatingPreparedStatement.java:1 05)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKey sDelegate.executeAndExtract(IdentityGenerator.java :94)
    at org.hibernate.id.insert.AbstractReturningDelegate. performInsert(AbstractReturningDelegate.java:57)
    ... 65 more
    I'm using Roo 1.2.1.RELEASE [rev 6eae723]
    Stephane

  2. #2
    Join Date
    Mar 2008
    Posts
    258

    Default

    Here is the table structure:

    | elearning_teacher | CREATE TABLE `elearning_teacher` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `version` int(10) unsigned NOT NULL,
    `user_id` int(10) unsigned NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `user_id_2` (`user_id`),
    UNIQUE KEY `id` (`id`),
    KEY `user_id` (`user_id`),
    CONSTRAINT `elearning_teacher_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=748 DEFAULT CHARSET=latin1 |
    Stephane

  3. #3
    Join Date
    Mar 2008
    Posts
    258

    Default

    One of the tests that fails:

    Code:
    privileged aspect ElearningTeacherIntegrationTest_Roo_IntegrationTest {
    
        declare @type: ElearningTeacherIntegrationTest: @RunWith(SpringJUnit4ClassRunner.class);
    
        declare @type: ElearningTeacherIntegrationTest: @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext*.xml");
    
        declare @type: ElearningTeacherIntegrationTest: @Transactional;
    
        @Autowired
        private ElearningTeacherDataOnDemand ElearningTeacherIntegrationTest.dod;
    
        @Test
        public void ElearningTeacherIntegrationTest.testCountElearningTeachers() {
            Assert.assertNotNull("Data on demand for 'ElearningTeacher' failed to initialize correctly", dod.getRandomElearningTeacher());
            long count = ElearningTeacher.countElearningTeachers();
            Assert.assertTrue("Counter for 'ElearningTeacher' incorrectly reported there were no entries", count > 0);
        }
    
    }
    Stephane

  4. #4
    Join Date
    Mar 2008
    Posts
    258

    Default

    I guess the problem lies with the unique key index

    UNIQUE KEY `user_id_2` (`user_id`)

    If I look at the domain class in the ElearningTeacher_Roo_DbManaged.aj file

    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
    private User ElearningTeacher.userId;

    we cam see that the JoinColumn annotation does not display the unique=true attribute.

    Is it not supported by Roo ?
    Last edited by stephaneeybert; Apr 8th, 2012 at 09:16 AM.
    Stephane

  5. #5
    Join Date
    Mar 2008
    Posts
    258

    Default

    I tried changing the annotation to a OneToOne as in:

    public class ElearningTeacher {

    @OneToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, unique=true)
    private User userId;

    }

    But I still get the same exception.
    Stephane

  6. #6
    Join Date
    Dec 2005
    Posts
    930

    Default

    Was your database empty before running the tests?
    Alan Stewart
    Spring Roo Committer
    twitter @alankstewart

  7. #7
    Join Date
    Mar 2008
    Posts
    258

    Default

    Yes, totally empty. It is being used for integration testing only.
    Stephane

  8. #8
    Join Date
    Dec 2005
    Posts
    930

    Default

    Would you please try with hsqldb or H2 in memory?
    Alan Stewart
    Spring Roo Committer
    twitter @alankstewart

  9. #9
    Join Date
    Mar 2008
    Posts
    258

    Default

    Hi Alan, thanks for your interest. I shall try it and come back to you.

    One question though, since a live database hold no schema before the application is loaded, I guess I cannot reverse engineer from it.

    Then, I wonder what step you want me to take for that try.

    Should I find a way to create the schema at loading time, from the domain classes ?
    Stephane

Posting Permissions

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