Hi,
I've come across an issue with the combination of embedded objects and inheritance. Given a parent class :
Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIM", discriminatorType = DiscriminatorType.STRING)
public abstract class Ape {
private Long _id;
private Head _head;
public Ape() {
_head = new Head();
}
@Id
public Long getId() {
return _id;
}
public void setId(Long id) {
_id = id;
}
@Embedded
public Head getHead() {
return _head;
}
public void setHead(Head head) {
_head = head;
}
}
and the embeddable class
Code:
@Embeddable
public class Head {
private String _eyeColour;
private Long _numberOfTeeth;
public Head() {
_eyeColour = "Blue";
_numberOfTeeth = 20l;
}
@Column(name = "EYE_COLOUR", nullable = false)
public String getEyeColour() {
return _eyeColour;
}
public void setEyeColour(String eyeColour) {
_eyeColour = eyeColour;
}
@Column(name = "NUMBER_OF_TEETH", nullable = false)
public Long getNumberOfTeeth() {
return _numberOfTeeth;
}
public void setNumberOfTeeth(Long numberOfTeeth) {
_numberOfTeeth = numberOfTeeth;
}
}
with, of course, the appropriate backing table, I can define various subclasses. One will suffice :
Code:
@Entity
@DiscriminatorValue("MONKEY")
public class Monkey extends Ape{
private Long _leapingHeight;
public Monkey() {
super();
}
@Column(name = "LEAPING_HEIGHT", nullable = false)
public Float getLeapingHeight() {
return _leapingHeight;
}
public void setLeapingHeight(Float leapingHeight) {
_leapingHeight = leapingHeight;
}
}
Anyway, if I create and save a Monkey, I can either retrieve by doing :
Code:
session.load(Monkey.class, monkeyId)
or
Code:
session.load(Ape.class, monkeyId)
Both of these work and, obviously, I can cast either to an Ape object. However, even though both Monkey and Ape should have access to the Head property, only the second of the two lookups works. The first, when you have retrieved the subclass, fails with a
Code:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [Monkey#0]
Does anyone have any idea what I might be doing wrong? There is nothing in the literature saying that an embedded property on a parent is not accessible from a subclass.
Hope someone with the Hibernate magic can shed some light on this :)