Hello,
I have a problem with TABLE_PER_CLASS inheritance mapping.
When loading only some properties of the parent class, some properties are not loaded as expected. Please see below.
Am I trying to do some unsupported mapping, or is this maybe a bug?
Thanks for your help in advance!
Regards:
Norbi
Version info:
hibernate-core: 3.3.1.GA
hibernate-annotations: 3.4.0.GA
hibernate-entitymanager: 3.4.0.GA
database: oracle 10g
Annotated entities:
Parent.java
Code:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Parent implements Serializable {
private int id;
private int parentValue;
private int childValue;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParentValue() {
return parentValue;
}
public void setParentValue(int parentValue) {
this.parentValue = parentValue;
}
@Transient
public int getChildValue() {
return childValue;
}
public void setChildValue(int childValue) {
this.childValue = childValue;
}
@Override
public String toString() {
return "[id=" + getId() + ", parentValue=" + getParentValue() + ", childValue=" + getChildValue() + "]";
}
}
Child1.javaCode:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Child1 extends Parent {
@Override
@Column(name = "childValueSomeName")
public int getChildValue() {
return super.getChildValue();
}
}
Child2.javaCode:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Child2 extends Parent {
@Override
@Column(name = "childValueSomeOtherName")
public int getChildValue() {
return super.getChildValue();
}
}
SQL:Code:
create table Child1 (id number(10,0) not null, parentValue number(10,0) not null, childValueSomeName number(10,0), primary key (id));
create table Child2 (id number(10,0) not null, parentValue number(10,0) not null, childValueSomeOtherName number(10,0), primary key (id));
Insert into CHILD1 (ID,PARENTVALUE,CHILDVALUESOMENAME) values (1 1 1);
Insert into CHILD2 (ID,PARENTVALUE,CHILDVALUESOMEOTHERNAME) values (2 2 2);
Loading entire objects works as expected:HQL: SELECT o FROM Parent o
Result:
[id=1, parentValue=1, childValue=1]
[id=2, parentValue=2, childValue=2]
Generated SQL:
Code:
select parent0_.id as id0_, parent0_.parentValue as parentVa2_0_, parent0_.childValueSomeName as childVal1_1_, parent0_.childValueSomeOtherName as childVal1_2_, parent0_.clazz_ as clazz_ from ( select id, null as childValueSomeOtherName, childValueSomeName, parentValue, 1 as clazz_ from Child1 union all select id, childValueSomeOtherName, null as childValueSomeName, parentValue, 2 as clazz_ from Child2 ) parent0_
But loading the overridden properties does not work:HQL: SELECT o.id, o.parentValue, o.childValue FROM Parent o
Result:
1 1 null
2 2 2
But the correct result would be:1 1 1
2 2 2
Generated SQL:
Code:
select parent0_.id as col_0_0_, parent0_.parentValue as col_1_0_, parent0_.childValueSomeOtherName as col_2_0_ from ( select id, null as childValueSomeOtherName, childValueSomeName, parentValue, 1 as clazz_ from Child1 union all select id, childValueSomeOtherName, null as childValueSomeName, parentValue, 2 as clazz_ from Child2 ) parent0_
Querying the subclasses directly works as expected, so
SELECT o.id, o.parentValue, o.childValue FROM Child1 o gives the correct result.