Hibernate version: hibernate-3.2
Annotation version: hibernate-annotations-3.2.0.CR2
Hi,
I'm trying now for days to solve my problem, which I'm not sure if it is a problem or expected behaviour, since it seems nobody else has this problem.
My problem is as follows:
I have an object called BillingUnit which contains many different types of costs. All costs inherit from the same base class. I use single-table inheritance with a descriminator.
When I call a cost method from BillingUnit to get a specific cost, I get all the costs and not just the correct cost type. It seems that in the SQL the descriminator does not get evaluated or is ignored, which makes me think that there is something wrong with my mapping!
Here is the data model, respectively the mappings. For simplicity only the relavent fields are included.
Code:
@Entity()
@Table(name = "AE")
public class BillingUnit implements DomainModel {
private Integer _id;
private Set<HeatingWarmwaterCostTree> _heatingWarmwaterCostTrees;
@Id
@Column(name = "AE_IDPK", nullable = false)
public Integer getId() {
return _id;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "KOST_DAID")
public Set<HeatingWarmwaterCostTree> getHeatingWarmwaterCostTrees() {
return _heatingWarmwaterCostTrees;
}
public void setHeatingWarmwaterCostTrees(Set<HeatingWarmwaterCostTree> heatingWarmwaterCostTrees) {
_heatingWarmwaterCostTrees = heatingWarmwaterCostTrees;
}
}
Code:
@Entity
@Table(name = "KOSTEN")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="KOST_HZWWORHNK", discriminatorType=DiscriminatorType.INTEGER)
public class CostTree implements DomainModel {
private Integer _id;
public CostTree() {
}
@Id
@Column(name = "KOST_IDPK")
public Integer getId() {
return _id;
}
public void setId(Integer id) {
_id = id;
}
}
Code:
@Entity
@DiscriminatorValue(value = "0")
public class HeatingWarmwaterCostTree extends CostTree {
public HeatingWarmwaterCostTree() {
}
}
I'm using Spring for Session management
Code:
public BillingUnit find(int id) throws DaoException {
try {
return (BillingUnit) getHibernateTemplate().load(BillingUnit.class, id);
} catch (DataAccessException e) {
throw new DaoException(e, LOG);
}
}
UnitTest
Code:
public void testBillingUnit() throws Exception {
BillingUnitService billingUnitService = getBean("billingUnitService");
BillingUnit billingUnit = billingUnitService.getBillingUnit(2499);
assertNotNull(billingUnit);
for (HeatingWarmwaterCostTree heatingWarmwaterCostTree : billingUnit.getHeatingWarmwaterCostTrees()) {
System.err.println(heatingWarmwaterCostTree.getDescription());
}
}
When I run the test it prints all the different costs which are associated with this billingunit, but actually there is only one HeatingWarmWaterCost in the DB.
Any help would be appreciated!