title: JPA Entity Inheritance with abstract classes - ConstrainViolationException
body: <p>I am trying to set up Entity Inheritance using JPA annotations and abstract classes. </p>
  
  <p>Our goal is to have the DAO work with the base object through its extension so that we can have mutations of the same application by using a different entity extension, pointcuts and overrides without having to change providers and managers.</p>
  
  <p>example:
  The base application stack has a DAO provider that uses an abstract Company entity to persist all objects that extend Company:</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  public abstract class Company  extends AbstractPersistable&lt;Long&gt; {
  
      @Column(unique = true)
      private String register_number;           
  
      // ...
  }
  
  @Component
  public class CompanyProvider extends JpaProvider&lt;Company, Long&gt; {
  // provides CRUD methods via an EntityManager
  }
  </code></pre>
  
  <p>(JPAProvider Setup for reference: )</p>
  
  <pre><code>public abstract class JpaProvider&lt;T, ID extends Serializable&gt; implements JpaRepository&lt;T, ID&gt;, JpaSpecificationExecutor&lt;T&gt;, QueryDslPredicateExecutor&lt;T&gt; {
  
      @PersistenceContext
      private EntityManager em;
  </code></pre>
  
  <p>Application A extends the Company to CompanyDefault adding its own columns but without changing the DAO. Application B should do the same:</p>
  
  <pre><code>@Entity
  @Table(name = "company")
  public class CompanyDefault  extends Company {    
  
      private String name;  
  
  }
  </code></pre>
  
  <p>The persistence.xml exists once for each application(or UnitTest) so this should define the actual entity:</p>
  
  <pre><code>&lt;persistence ... &gt;
      &lt;persistence-unit name="persistanceUnit"  transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;class&gt;path.to.CompanyDefault&lt;/class&gt;
  
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Now to create a Company I do the following:</p>
  
  <pre><code>@Inject
  private CompanyProvider companyProvider;    
  
  @Test
  public void testAbstractCompany() {
      Company company = new CompanyDefault("123");
      ((CompanyDefault)company).setName("test");
  
      companyProvider.save(company);
  }
  </code></pre>
  
  <p>The unsatisfying result is a ConstraintVioloationException telling me I have a duplicate entry for the primary key:</p>
  
  <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [...CompanyDefault]
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1154)
      at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
      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:597)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
      at $Proxy37.persist(Unknown Source)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:328)
      at ....JpaProvider.save(JpaProvider.java:287)
      at ....JpaProvider$$FastClassByCGLIB$$2caf68f6.invoke(&lt;generated&gt;)
      at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617)
      at ....CompanyProvider$$EnhancerByCGLIB$$d3a45b31.save(&lt;generated&gt;)
      at ....PersonManagerTest.testAbstractCompany(PersonManagerTest.java:111)
      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:597)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [....CompanyDefault]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2454)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2854)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
      at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
      at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
      at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
      at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
      at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
      at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
      at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
      at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
      ... 42 more
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '8' for key 'PRIMARY'
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
      at com.mysql.jdbc.Util.getInstance(Util.java:381)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1038)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3563)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3495)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2693)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2102)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2395)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2313)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2298)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2437)
      ... 56 more
  </code></pre>
  
  <p>Now why would hibernate try to persist the same entity twice? </p>
  
  <p>What about my Setup and the annotations? </p>
  
  <p>Is what we want to do possible to do: Saving entities by using extensions of an abstract class?</p>
  
  <p>Thanks a lot for your help!</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>The same happens if I create and save the extended object directly:</p>
  
  <pre><code>@Test
  public void testAbstractCompany() {      
      CompanyDefault companyDefault = new CompanyDefault("123");
      companyProvider.save(companyDefault);
  }
  </code></pre>
  
  <p><strong>Edit2 - Solved:</strong></p>
  
  <p>Never mind my text wall, solved it already.. Just a simple mistake copy-pasing google results:</p>
  
  <p>Have to use <code>InheritanceType.SINGLE_TABLE</code> obviously. I'll close this as soon as I can.</p>