I have mapped an inheritance hierarchy using the 'Joined subclasses' strategy for the classes ChildType1, ChildType2 and Parent (ChildType1 and ChildType2 extends Parent). The subclasses have a primary key association to the Parent superclass making it a one-to-one association. There is another class that I am calling A. A has a bidirectional one-to-many relationship with ChildType1 and ChildType2.
Code:
@Entity
class A {
List<ChildType1> childrenType1 = new ArrayList<ChildType1>;
List<ChildType2> childrenType2 = new ArrayList<ChildType2>;
@OneToMany(mappedBy="a", targetEntity=Parent.class, fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade( {
org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@IndexColumn(base=0, nullable=false, name="idx")
public List<ChildrenType1> getChildrenType1();
@OneToMany(mappedBy="a", targetEntity=Parent.class, fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade( {
org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@IndexColumn(base=0, nullable=false, name="idx")
public List<ChildrenType2> getChildrenType2();
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
abstract class Parent {
A a;
@ManyToOne
@JoinColumn(name="some_fk", nullable=false)
public A getA();
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name="id")
class ChildType1 extends Parent {
...
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name="id")
class ChildType2 extends Parent {
...
}
When I call
getChildrenType1 I noticed the following hibernate query was generated (I have changed the names of the classes used and removed the
as value_0_, essentially I have the 2 subclasses and the superclass mapped using the 'table-per-subclass':
Code:
select child0_.some_fk, child0_1_.value, child0_2_.value,
case
when child0_1_.id is not null then 1
when child0_2_.id is not null then 2
when child0_.id is not null then 0
end as clazz_0_ from
Parent child0_ left outer join ChildType1 child0_1_ on child0_.id=child0_1_.id
left outer join ChildType2 child0_2_ on child0_.id=child0_2_.id
where child0_.some_fk=?
I want to
ONLY get the
ChildType1 back - I can't see why the case statement is necessary.
Some help would be very welcome. I have read and re-read the documentation and the forums. I am starting to think that perhaps mapping an inheritance hierarchy is more trouble than it's worth (that is unless I am doing something wrong)...