Reputation: 724
I have the following Hibernate classes:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class User {
@Id
private Long id;
}
@Entity
public class Patient extends User {
/* ... */
}
@Entity
public class Doctor extends User {
/* ... */
}
During the login phase, I execute
User user = db.createQuery("from User where email = :email")
.setParameter("email", email)
.uniqueResult();
Unluckily, user
is always a proxy of User
, so I can't cast it to Doctor
.
Can I force Hibernate to look closely at the user type and return the right proxy, possibly without executing another query?
Upvotes: 3
Views: 838
Reputation: 691765
I think it should return the right subclass instance if this user is not yet loaded as a User (due to a previously loaded association, for example).
If not, then you have no way of letting it return an actual Doctor instance. So you should either call a polymorphic method of User, or using proprietary Hibernate code to check if it's a proxy, initialize it and get the wrapped object.
If you know in advance that it's a Doctor, then use "from Doctor" rather than "from User".
Upvotes: 1