Announcement Announcement Module
Collapse
No announcement yet.
Integration test causing a duplicate entry exception on insert Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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]

  • #2
    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 |

    Comment


    • #3
      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);
          }
      
      }

      Comment


      • #4
        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, 09:16 AM.

        Comment


        • #5
          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.

          Comment


          • #6
            Was your database empty before running the tests?

            Comment


            • #7
              Yes, totally empty. It is being used for integration testing only.

              Comment


              • #8
                Would you please try with hsqldb or H2 in memory?

                Comment


                • #9
                  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 ?

                  Comment


                  • #10
                    One question though, since a live database hold no schema before the application is loaded, I guess I cannot reverse engineer from it.
                    You can use the reverse-engineered MySQL schema to create the schema in hsql or H2 with a unit test or web app. Just remember to turn on the Hibernate hibernate.hbm2ddl.auto to "create", as dbre will change it to "validate" to prevent production DBs from being changed.

                    Comment


                    • #11
                      If you have a link to an example or blog article on creating a schema from within an integration test, I could make good use of it, otherwise no worries, I'll google.

                      Comment


                      • #12
                        Simply running your unit tests or spinning up a Roo-generated MVC web app will cause Hibernate to create the schema in the DB

                        Comment


                        • #13
                          Sorry for being so slow Alan, do I do this in the roo shell ? Is there a command to do it ?

                          I have reversed engineered the database schema but still wonder how to change the data source.

                          Comment


                          • #14
                            You can run "perform tests" in the Roo shell, or mvn test from the command line. To run the web app, do a mvn tomcat:run, providing you have generated the web tier.

                            To change the persistence setup, run "jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY". It's all in the reference docs.

                            Comment


                            • #15
                              Oh ! Thanks Alan, I should have guessed it, have used the following script before:

                              Code:
                              project --topLevelPackage com.learnintouch.lms --packaging POM
                              module create --moduleName core --topLevelPackage com.learnintouch.lms.core
                              module create --moduleName ui --topLevelPackage com.learnintouch.lms.ui --packaging POM
                              module create --moduleName vaadin --topLevelPackage com.learnintouch.lms.ui.vaadin
                              
                              module focus --moduleName core
                              jpa setup --provider HIBERNATE --database MYSQL
                              database properties list
                              database properties set --key database.username --value learnintouch
                              database properties set --key database.password --value mignet
                              database properties set --key database.url --value jdbc:mysql://localhost:3306/db_integration
                              database properties list
                              database introspect --schema db_integration
                              addon install id --searchResultId 01
                              database reverse engineer --package ~.domain --schema PUBLIC --testAutomatically --includeNonPortableAttributes
                              
                              module focus --moduleName core
                              service --interface ~.service.PreferenceService --entity ~.domain.Preference
                              So it'll be a breeze I guess to change the data source now, more soon..

                              Comment

                              Working...
                              X