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.