javahibernateinheritancediscriminator

Limit the set of selected fields to those of the target subclass


I'm using single table inheritance, one parent entity:

@Entity
@Table(name = "parent")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public class Parent {
  // ...
}

and two child entities:

@Entity
@DiscriminatorValue("child1")
public class Child1 extends Parent {
  @Column(name = "child1_property")
  private Integer child1Property;
  // ...
}
@Entity
@DiscriminatorValue("child2")
public class Child2 extends Parent {
  @Column(name = "child2_property")
  private Integer child2Property;
  // ...
}

Now, if I query (HQL) directly from Child1 entity:

from Child1

it will generate an SQL that selects only the columns from Parent entity plus Child1 entity. If I select from Parent entity using a where type='child1'

from Parent p where type(p)='child1'

it will return only Child1 entities but the SQL query selects all columns from Parent, Child1 and Child2. Is there a possibility to query from Parent entity use the discriminator column (i.e. limit only to Child1 entities) and get a specific SQL query, i.e. that will select only columns from Parent and Child1.


Solution

  • A projection seems to work:

    SELECT new fully.qualified.name.of.Child1(p.id, p.name, p.child1Property)
      FROM Parent p
     WHERE TYPE(p) = 'child1'
    

    produces

        select
            parent0_.id as col_0_0_,
            parent0_.name as col_1_0_,
            parent0_.child1_property as col_2_0_ 
        from
            parent parent0_ 
        where
            parent0_.type='child1'
    

    Hibernate User Guide excerpt:

    There is a particular expression type that is only valid in the select clause. Hibernate calls this "dynamic instantiation". JPQL supports some of that feature and calls it a "constructor expression".

    So rather than dealing with the Object[] (again, see Hibernate Query API) here, we are wrapping the values in a type-safe Java object that will be returned as the results of the query.

    ...

    The projection class must be fully qualified in the entity query, and it must define a matching constructor.

    The class here need not be mapped. It can be a DTO class.

    If it does represent an entity, the resulting instances are returned in the NEW state (not managed!).

    However, I'd just SELECT c FROM Child1 c. I was actually surprised that Hibernate didn't complain about the attribute child1Property missing for the entity Parent.