title: Hibernate inheritance and multiple tables with same class
body: <p>I'm pretty new to Hibernate. In my situation I have a concrete table with records containing join IDs to a multitude of other tables - all with the same structure.
  What I'd like to achieve is get something like </p>
  
  <pre><code>SELECT * 
    FROM main_records mr, ref1 r1, ref2 r2 
   WHERE r1.id = mr.id_ref1 
     AND r2.id = mr.id_ref2;
  </code></pre>
  
  <p>The main idea would be to reuse the class for all the join references.</p>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>CREATE TABLE main_records
  (
    id integer NOT NULL,
    id_ref1 integer NOT NULL,
    id_ref2 integer NOT NULL
  )    
  CREATE TABLE ref1
  (
    id integer NOT NULL,
    value character varying
  )
  CREATE TABLE ref2
  (
    id integer NOT NULL,
    value character varying
  )
  </code></pre>
  
  <p>I've setup the base POJO classes</p>
  
  <p><strong>JAVA classes</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainRecord {
    private Integer id;
    private Ref ref1;
    private Ref ref2;
    ...
    // getters and setters
  }
  
  public class Ref {
    private Integer id;
    private String value;
    ...
    // getters and setters
  }
  </code></pre>
  
  <p>My idea is to define the Hibernate mappings in following manner:</p>
  
  <p><strong>Define an abstract super class</strong></p>
  
  <pre><code>&lt;hibernate-mapping package="test"&gt;
      &lt;class abstract="true" name="Ref"&gt;
          &lt;id name="id" type="java.lang.Integer" column="ID"&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
          &lt;property name="value" type="java.lang.String" column="VALUE" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>Map main entity, extend the super class but use individual tables</strong></p>
  
  <pre><code>&lt;hibernate-mapping package="test"&gt;
      &lt;union-subclass name="Ref1" table="REF1" extends="Ref" /&gt;
      &lt;union-subclass name="Ref2" table="REF2" extends="Ref" /&gt;
  
      &lt;class name="MainRecord" table="MAIN_RECORDS"&gt;
          &lt;id name="id" column="ID" type="java.lang.Integer" /&gt;
          &lt;many-to-one name="ref1" class="Ref1" column="ID_REF1" fetch="join" unique="true" /&gt;
          &lt;many-to-one name="ref2" class="Ref2" column="ID_REF2" fetch="join" unique="true" /&gt;
       &lt;/union-subclass&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>I do manually include mapping files in the configuration, loading seems ok but then an error occurs, without any detaild explanation:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.transport.servlet.ServletTransportFactory' defined in class path resource [META-INF/cxf/cxf-servlet.xml]: Error setting property values; 
  nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
  PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/database.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
  PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/database.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
  </code></pre>
  
  <p>The system is a mix of Spring 2.5, Hibernate 3.2, Cxf 2.3.4, Javassist 3.11, </p>
  
  <p>My questions are:</p>
  
  <p>(a) Is this the correct approach?</p>
  
  <p>(b) The error occurs as soon as I introduce </p>
  
  <pre><code>&lt;union-subclass name="Ref1" table="REF1" extends="Ref" /&gt;
  &lt;union-subclass name="Ref2" table="REF2" extends="Ref" /&gt;
  </code></pre>
  
  <p>so I guess this is not the best way of doing it?</p>
  
  <p>(c) Can it be written with annotations? I cannot fathom how to define the Ref1 and Ref2 classes without actually creating a POJO class for them.
  (d) Can I user more than 1 level of inheritance? I'd like, for example, use an abstract superclass for all my concrete tables, that cover the auditing fields they all have in common?
  Let's say class Ref extends an abstract AuditTable class, both in Java and Hibernate mappings.</p>