title: JPA entities: inheritance and one-to-one relationship not working simultaneously
body: <p>I have the following scheme: TableA1 and TableA2 exist in the database and each is represented by an entity bean. Since they are related, I created an abstract class (TableA, it's an entity but does not exist in the database) where both entities inherit from this class. In addition, TableA has a one-to-one relationship with TableB. </p>
  
  <p>My objective is to query TableB and from there get information of TableA1 or TableA2 depending on the type.</p>
  
  <p>TableA1 and TableA2 each has an id (each table generates automatically a sequential number, so you may have repetition).</p>
  
  <p>In TableB I have two columns that combined represent the foreign key: type and id. Type = 1 means that id is in TableA1. Similarly with TableA2.</p>
  
  <p>My problem is that I don't know how to define these two columns as an external foreign key.
  This is what I've got:</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  @DiscriminatorColumn(name="type")
  public abstract class TableA { 
  
      @Id
      @Column(name = "type")
      protected int type;
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      protected int id;
  
      @Column(name = "name")
      private String name;
  
      // Getters and setters
  
  }
  
  
  @Entity
  @DiscriminatorValue("1")
  @Table (name="tableA1")
  public class TableA1 extends TableA {
  
      @Column(name="col1")
      private String col1;
  
      // Getters and setters
  
  }
  
  
  @Entity
  @DiscriminatorValue("2")
  @Table (name="tableA2")
  public class TableA2 extends TableA {
  
      @Column(name="col2")
      private String col2;
  
      // Getters and setters
  }
  
  
  @Entity
  @Table (name="tableB")
  public class TableB {
  
      @Id 
      @Column(name="someId")
      private Integer someId;
  
  
      @Column(name="type")
      private int type;
  
      @Column(name="id")
      private Integer id;   
  
  
      @OneToOne(optional = false, cascade = CascadeType.ALL)
      @JoinColumns({    
          @JoinColumn(name = "type"),
          @JoinColumn(name = "id" )
        })
      private TableA tableA;
  
      // Getters and setters
  
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Am I looking for the impossible? This is what I found:</p>
  
  <blockquote>
    <p>Polymorphic relations to non-leaf classes in a table-per-class hierarchy have many limitations. When the concrete subclass is not known, the related object could be in any of the subclass tables, making joins through the relation impossible. This ambiguity also affects identity lookups and queries; these operations require multiple SQL SELECTs (one for each possible subclass), or a complex UNION.</p>
  </blockquote>
  
  <p><strong>Update 2</strong></p>
  
  <p>TableA1, TableA2 and TableB <strong>already exist</strong> in the database and have the following structure:</p>
  
  <pre><code> CREATE TABLE TableA1 (
     surrogate_key int AUTO_INCREMENT,
     some_char char(30),
     PRIMARY KEY (surrogate_key)
   );
  
  CREATE TABLE TableA2 (
     surrogate_key int AUTO_INCREMENT,
     some_int int,
     PRIMARY KEY (surrogate_key)
  );
  
  CREATE TABLE TableB (
     surrogate_key int AUTO_INCREMENT,
     type int,  // if type=1, sk represents the surrogate_key of tableA1
                // if type=2, sk represents the surrogate_key of tableA2
     sk int,
     description varchar(200),
     PRIMARY KEY (surrogate_key)
   );
  </code></pre>