Hi,
I use JPA / Hibernate (3.2.6.GA) and I encounter a problem using inheritance and entities.
I have 4 entities :
- Population
- PopulationCriterion, abstract one
- SimpleExpression, that extends PopulationCriterion
- LogicalExpression, that extends PopulationCriterion
Population entity has a "rootCriterion" property, that points to a population criterion that can be either a simple expression instance, either a logical expression instance.
Here is a picture of the model :
I run the following unit test :
Code:
Population population = dao.findByCode("POP_1");
assert population.getId() != null;
assert "POP_1".equals(population.getCode());
assert population.getRootCriterion() != null;
assert population.getRootCriterion() instanceof SimpleExpression;
SimpleExpression rootCriterion = (SimpleExpression) population.getRootCriterion();
But it fails on "instanceof". Using getClass().getName() I can see that rootCriterion property is of PopulationCriterion type, while it is an abstract class ! I expected it to be either SimpleExpression, either LogicalExpression, but not PopulationCriterion !
I understood that Hibernate supported inheritance, so I can't understand why it has problem with this. Or maybe there is something that I misunderstood ?
Here are some interesting extracts (I think !) of my entities :
Population.java :
Code:
@Entity
public class Population
{
...
private PopulationCriterion rootCriterion;
@ManyToOne(optional = false, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "ROOT_CRITERION_FK_")
public PopulationCriterion getRootCriterion()
{
return this.rootCriterion;
}
...
}
PopulationCriterion.java :
Code:
@Entity
public abstract class PopulationCriterion
{
...
}
SimpleExpression.java :
Code:
@Entity
public class SimpleExpression
extends PopulationCriterion
{
...
}
LogicalExpression.java :
Code:
@Entity
public class LogicalExpression
extends PopulationCriterion
{
...
}
I desperately try to understand what happens since yesterday, so any help is very welcome ;)
Thanks in advance
Olivier