Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA : Inheritance - Discriminator value not taken into account in generated SQL

I try to use this mapping :

@Entity
@Table(name="ecc.\"RATE\"")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIMINATOR", discriminatorType= DiscriminatorType.STRING)
public abstract class Rate extends GenericBusinessObject {
...
}

@Entity
@DiscriminatorValue("E")
public class EntranceRate extends Rate { 
 @ManyToOne
 @JoinColumn(name = "\"RATES_GRID_ID\"")
 protected RatesGrid ratesGrid;
...
}


@Entity
@Table(name="ecc.\"RATES_GRID\"")
public class RatesGrid extends GenericBusinessObject {
 /** */
 @OneToMany(mappedBy = "ratesGrid",  targetEntity = EntranceRate.class, fetch=FetchType.LAZY)
 private List<EntranceRate> entranceRates;
}

When I try to access my entranceRates list from a ratesGrid object, I get this error :

Object with id: 151 was not of the specified subclass: com.ecc.bo.rate.EntranceRate (loaded object was of wrong class class com.ecc.bo.rate.AnnualRate)

Looking at the sql generated, I found no trace of "discriminator=" in the where clause. What am I doing wrong ?

I use a PostGreSQL database and a Hibernate as JPA provider.

like image 650
Julien Avatar asked Apr 12 '10 08:04

Julien


1 Answers

I don't know if this is a bug or a feature (for me, it's a bug), but the solution (workaround?) is to use the Hibernate annotation @ForceDiscriminator on your top class:

@Entity
@Table(name="ecc.\"RATE\"")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISCRIMINATOR", discriminatorType= DiscriminatorType.STRING)
@org.hibernate.annotations.ForceDiscriminator
public abstract class Rate extends GenericBusinessObject {
    ...
}

You might want to vote for HHH-4358.

like image 142
Pascal Thivent Avatar answered Sep 27 '22 19:09

Pascal Thivent