Hibernate version: 3.2.6 GA
Name and version of the database you are using: SQL Server 2005
Imagine these two hypothetical class hierarchies...
Code:
Automobile
|
|-----------|
Car Truck
TireType
|
|-----------|
Offroad Onroad
Code:
@Entity
@Table(name = "automobile")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class Automobile
{
@Transient
public abstract TireType getTireType();
}
@Entity
@DiscriminatorValue("CAR")
public class Car extends Automobile
{
private Onroad tireType;
@ManyToOne(cascade = {})
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "tire_type_id")
@NotNull
public Onroad getTireType()
{
return tireType;
}
}
@Entity
@DiscriminatorValue("TRUCK")
public class Truck extends Automobile
{
private Offroad tireType;
@ManyToOne(cascade = {})
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "tire_type_id")
@NotNull
public Offroad getTireType()
{
return tireType;
}
}
Code:
@Entity
@Table(name = "tire_type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class TireType
{
....
}
@Entity
@DiscriminatorValue("ON")
public class Onroad extends TireType
{
....
}
@Entity
@DiscriminatorValue("OFF")
public class Offroad extends TireType
{
....
}
Now let's say Automobile #1 is a [Car] with an [Onroad] tire type. If I call the following:
session.get(Automobile.class, 1);
I get an Exception like:
Code:
org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: Offroad (Discriminator: ON)
It's basically saying that my Car's tiretype property is mapped as an Offroad tire type but pointing to an Onroad one. However, is NOT. The message shows the wrong class type, but the correct discriminator.
This DOES work.
session.get(Car.class, 1);
I've quadruple checked the database, and the discriminators are all correct. Did I hit a limitation in the polymorphic capabilities or should I be able to do things like this? The reality of my structure is a bit more complicated (more subclass layers, etc.). I've gone through it all extensively, but I'm out of ideas.
Any suggestions? Thanks.