title: Hibernate @OneToOne with superclass, only retrieve superclass fields on join
body: <p>I have mapped my inheritance hierarchy in Hibernate using InheritanceType.Single_Table and discriminator columns to distinguish between the different entities. All subclasses of the superclass store their fields into secondary tables. As an example:</p>
  
  <pre><code>@MappedSuperclass
  public abstract class Base
  {
     @Id 
     private String id;
  
     @Version
     private long version;
  }
  
  @Entity
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING) 
  public class Parent extends Base
  {
      @Column(nullable=false)
      private BigDecimal value;
  }
  
  @Entity
  @DiscriminatorValue("child1")
  @SecondaryTable(name = "Child1")
  public class Child1 extends Parent
  {
       @Column(table="Child1")
       private String name;
  }
  
  @Entity
  @DiscriminatorValue("child2")
  @SecondaryTable(name = "Child2")
  public class Child2 extends Parent
  {
       @Column(table="Child2")
       private String name2;
  } 
  </code></pre>
  
  <p>I now have an Entity that has a @OneToOne relationship with the Parent class. This Entity only needs to work with the value field from the Parent class. It will never need to work with any fields from any subclass of Parent</p>
  
  <pre><code>@Entity
  public class AnotherEntity extends Base
  {
      @JoinColumn(name="parentId")
      @OneToOne(fetch=FetchType.Lazy, optional=true, targetEntity=Parent.class)
      private Parent parent;
   }
  </code></pre>
  
  <p>What I want to happen is that only the fields of Parent.class are selected when the relationship to parent is loaded from the database. What I'm seeing is that Hibernate attempts to load all properties of the entities that extend Parent. It also left joins all of the Secondary tables. This is problematic as I have rougly 30 entities that extend Parent. This makes fetching the Parent entity non-viable as the query performs 30 joins.</p>
  
  <p>As an example, this is the type of query I am seeing:</p>
  
  <pre><code>Hibernate: 
  select
      parent.id as id3_0_,
      parent_.version as version3_0_,
      parent.name1 as name110_3_0_,
      parent.name2 as name24_3_0_,
      parent.type as type3_0_ 
  from
      Parent parent0_ 
  left outer join
      Child1 parent0_2_ 
          on parent0_.id=parent0_2_.id 
  left outer join
      Child2 parent0_3_ 
          on parent0_.id=parent0_3_.id 
  </code></pre>
  
  <p>I don't understand why Hibernate decides to select a superset of all properties defined in the subclasses of Parent and join all of the secondary tables? I could understand it joining the secondary table for entity defined by the discriminator value of the parent being referenced, but otherwise I am confused.</p>
  
  <p>My question is, how do I go about achieving my requirement of only having the fields from the Parent class loaded when I retrieve the Parent relationship in the AnotherEntity class?</p>
  
  <p>Thanks.</p>