title: Hibernate 5 assigning id that already exists in DB as preinitialization - org.springframework.dao.DataIntegrityViolationException
body: <p>I was planning to migrate from Spring 4.2.1.RELEASe + Hibernate 4.2.19.Final to 4.2.1.RELEASe + Hibernate 5.0.7.Final.</p>
  
  <p>I have created a simple project, <a href="https://github.com/codependent/spring4-hibernate5" rel="nofollow">available on Github</a>, to test the new Hibernate version and have come across a problem: in my integration tests I initialize a HSQLDB with two rows from a sql file. The point is that when my Hibernate version is 4.2.19.Final all the tests from <code>ExampleIntegrationTRTest</code> pass. However with 5.0.7.Final the very first test (<code>testExampleInsertWithRollback</code>) fails:</p>
  
  <p><strong>scripts/initData.sql</strong></p>
  
  <pre><code>INSERT INTO TFOEMPLE(FOEMPLE_ID, FOEMPLE_NAME) values(1, 'Louis');
  INSERT INTO TFOEMPLE(FOEMPLE_ID, FOEMPLE_NAME) values(2, 'Maria');
  </code></pre>
  
  <p><strong>ExampleIntegrationTRTest.testExampleInsertWithRollback</strong>: This test passes with 4.1, but fails due to a <code>javax.persistence.EntityExistsException</code> in 5.0.7.Final. Apparently it tries to create the new FooEmployee assigning it an id 1, which is already present in the DB as part of the initialization.</p>
  
  <pre><code>@ContextConfiguration({ "/spring/test/test-context.xml" })
  @Test
  public class ExampleIntegrationTRTest extends AbstractTransactionalTestNGSpringContextTests {
  
      @Transactional
  public void testExampleInsertWithRollback() {
  
      FooEmployee response = null;
      response = fooUserService.getEmployee(1);
      //response is FooEmpleado ( BaseEntity ( FooEmployee@590013c7    id = 1     )    id = 1    nombre = Louis     )
  
      FooEmployee newFoo = new FooEmployee();
      newFoo.setName("James");
      createdFooWithRollback = fooUserService.createEmployee(newFoo).getId();
      //The line above throws the exception
  
      assertNotNull(createdFooWithRollback);
      EntityManagerHolder holder = (EntityManagerHolder)TransactionSynchronizationManager.getResource(entityManagerFactory);
      EntityManager em = holder.getEntityManager();
      em.flush();
  
      em.clear();
  
      response = null;
      response = fooUserService.getEmployee(createdFooWithRollback);
  
      assertNotNull(response);
      assertEquals(response.getId(), createdFooWithRollback);
      assertEquals(response.getName(), "James");
  
  }
  </code></pre>
  
  <p><strong>Stacktrace</strong></p>
  
  <pre><code>org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [com.codependent.s4h5.entity.FooEmployeeEntity#1]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.codependent.s4h5.entity.FooEmployeeEntity#1]
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:410)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:223)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy52.save(Unknown Source)
      at com.codependent.s4h5.service.impl.FooUserServiceImpl.createEmployee(FooUserServiceImpl.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy57.createEmployee(Unknown Source)
      at com.codependent.s4h5.ExampleIntegrationTRTest.testExampleInsertWithRollback(ExampleIntegrationTRTest.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
      at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:200)
      at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:175)
      at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:212)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:689)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:882)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1189)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
      at org.testng.TestRunner.privateRun(TestRunner.java:767)
      at org.testng.TestRunner.run(TestRunner.java:617)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
      at org.testng.SuiteRunner.run(SuiteRunner.java:254)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
      at org.testng.TestNG.run(TestNG.java:1057)
      at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
  Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.codependent.s4h5.entity.FooEmployeeEntity#1]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1664)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:293)
      at com.sun.proxy.$Proxy47.persist(Unknown Source)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:439)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      ... 49 more
  </code></pre>
  
  <p><strong>Spring's test-context.xml</strong></p>
  
  <pre><code>&lt;tx:annotation-driven order="0" /&gt;
  
  &lt;!-- Drives transactions using datasource transaction manager --&gt;
  &lt;bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="mainEntityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="mainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="mainPersistenceUnit"/&gt;
      &lt;property name="dataSource" ref="mainDataSource"/&gt;
      &lt;property name="packagesToScan" value="com.codependent.s4h5.entity"/&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.import_files"&gt;scripts/initData.sql&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;/property&gt;
      &lt;property name="jpaDialect"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;import resource="classpath:com/codependent/s4h5/spring/library.xml" /&gt;
  
  &lt;jdbc:embedded-database id="mainDataSource" type="HSQL"/&gt;
  
  &lt;jpa:repositories base-package="com.codependent.s4h5.dao" entity-manager-factory-ref="mainEntityManagerFactory"/&gt;
  
  &lt;context:component-scan base-package="com.codependent.s4h5.service.impl" /&gt;
  &lt;context:component-scan base-package="com.codependent.s4h5.mapper" /&gt;
  &lt;bean id="dozerMapper" class="org.dozer.spring.DozerBeanMapperFactoryBean"/&gt;
  </code></pre>
  
  <p><strong>Project pom.xml</strong></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.codependent.s4h5&lt;/groupId&gt;
      &lt;artifactId&gt;spring4-hibernate5&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;description&gt;Test&lt;/description&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;!-- SPRING DATA --&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-data-releasetrain&lt;/artifactId&gt;
                  &lt;version&gt;${spring-data-releasetrain-version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;dependencies&gt;
          &lt;!-- SPRING --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- HIBERNATE --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- DOZER --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;net.sf.dozer&lt;/groupId&gt;
              &lt;artifactId&gt;dozer&lt;/artifactId&gt;
              &lt;version&gt;5.5.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;net.sf.dozer&lt;/groupId&gt;
              &lt;artifactId&gt;dozer-spring&lt;/artifactId&gt;
              &lt;version&gt;5.5.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- log system: slf4j --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${slf4j-version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
              &lt;version&gt;${slf4j-version}&lt;/version&gt;
              &lt;optional&gt;true&lt;/optional&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- TEST dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
              &lt;version&gt;${spring-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.testng&lt;/groupId&gt;
              &lt;artifactId&gt;testng&lt;/artifactId&gt;
              &lt;version&gt;${testng-version}&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;junit&lt;/groupId&gt;
                      &lt;artifactId&gt;junit&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-api-mockito&lt;/artifactId&gt;
              &lt;version&gt;${powermock-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-testng&lt;/artifactId&gt;
              &lt;version&gt;${powermock-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
              &lt;artifactId&gt;hsqldb-j5&lt;/artifactId&gt;
              &lt;version&gt;2.2.4&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
              &lt;version&gt;${log4j-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
              &lt;version&gt;${log4j-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
              &lt;version&gt;${log4j-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.jboss.logging&lt;/groupId&gt;
              &lt;artifactId&gt;jboss-logging&lt;/artifactId&gt;
              &lt;version&gt;${jboss-logging-version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.kubek2k&lt;/groupId&gt;
              &lt;artifactId&gt;springockito-annotations&lt;/artifactId&gt;
              &lt;version&gt;1.0.9&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;         
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
  
      &lt;properties&gt;
          &lt;spring-version&gt;4.2.1.RELEASE&lt;/spring-version&gt;
          &lt;spring-data-releasetrain-version&gt;Gosling-SR1&lt;/spring-data-releasetrain-version&gt;
          &lt;hibernate-version&gt;4.2.19.Final&lt;/hibernate-version&gt;
          &lt;testng-version&gt;6.8.17&lt;/testng-version&gt;
          &lt;powermock-version&gt;1.6.2&lt;/powermock-version&gt;
          &lt;slf4j-version&gt;1.7.7&lt;/slf4j-version&gt;
          &lt;log4j-version&gt;2.2&lt;/log4j-version&gt;
          &lt;jboss-logging-version&gt;3.2.1.Final&lt;/jboss-logging-version&gt;
      &lt;/properties&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>BaseEntity.java</strong></p>
  
  <pre><code>@SuppressWarnings("serial")
  public abstract class BaseEntity&lt;IdType extends Serializable&gt; implements Serializable {
  
      public abstract IdType getId();
  
      public abstract void setId(IdType id);
  
  }
  </code></pre>
  
  <p><strong>FooEmployeeEntity.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="TFOEMPLE")
  public class FooEmployeeEntity extends BaseEntity&lt;Integer&gt; {
  
      private static final long serialVersionUID = 2583160744316336577L;
  
      @Id
      @Column(name="FOEMPLE_ID")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id ;
  
      @Column(name="FOEMPLE_NAME")
      private String name;
  
      @ManyToOne
      @JoinColumn(name="FOEMPLE_FODEPAR_ID")
      private FooDepartmentEntity department;
  
      //Getters-Setters
  </code></pre>
  
  <p><strong>FooDepartmentEntity.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="TFODEPAR")
  public class FooDepartmentEntity extends BaseEntity&lt;Integer&gt; {
  
      private static final long serialVersionUID = 2583160744316336577L;
  
      @Id
      @Column(name="FODEPAR_ID")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id ;
  
      @Column(name="FODEPAR_NAME")
      private String name;
  
      @OneToMany(mappedBy="department",cascade={CascadeType.MERGE,CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.DETACH})
      private List&lt;FooEmployeeEntity&gt; employees;
  
      //Getters-Setters
  </code></pre>