Yeah. As far as I can understand, this isn't a problem, it's a feature of Hibernate.
If you really need to emulate polymorphism with Hibernate, you can use the Visitor pattern:
Code:
public interface PersonVisitor {
public Object visit(Person p);
public Object visit(User u);
}
public class Person {
// ...
public Object accept(PersonVisitor v) {
return v.visit(this);
}
}
public class User extends Person {
// ...
public Object accept(PersonVisitor v) {
return v.visit(this);
}
}
// ...
Person somePerson = session.get(User.class,new Long(id));
String result = (String)somePerson.accept(new PersonVisitor() {
public Object visit(Person p) { return "I'm a Person !"; }
public Object visit(User u) { return "I'm a User ! Yar !"; }
});
The code above is roughly equivalent to the following code, which will NOT work with Hibernate:
Code:
Person somePerson = session.get(User.class,new Long(id));
String result;
if(somePerson instanceof User) {
result = "I'm a User ! Yar !";
} else if(somePerson instanceof Person) {
result = "I'm a Person !";
}