我有一个Account
抽象类,它由另外两个类继承:
DoctorAccount
,NurseAccount
(为了保持简短的发布,我省略了getter和setter)。执行方法checkIfLoginAndPasswordIsCorrect
后,我收到错误:
Caused by: org.hibernate.WrongClassException: Object [id=1] was not of the specified subclass [Model.Account] : Discriminator: 1
。我发现query.getSingleResult();
对此负有责任,但我不知道为什么会抛出这样的错误。使用多态性我可以将Account分配给Doctor / Nurse。你知道我做错了吗?
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Entity
@DiscriminatorColumn(name = "Type_of_account", discriminatorType =
DiscriminatorType.STRING)
public abstract class Account {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Id")
private int id;
@Column(name="Login")
private String login;
@Column(name="Password")
private String password;
}
@Entity
public class NurseAccount extends Account{
}
@Entity
public class DoctorAccount extends Account{
}
在logInDAO类中:
@Override
public boolean checkIfLoginAndPasswordIsCorrect(String login, String password) {
Account account = null;
EntityManagerFactory managerFacotry = Persistence.createEntityManagerFactory("manager");
EntityManager manager = managerFacotry.createEntityManager();
manager.getTransaction().begin();
Query query = manager.createQuery("SELECT a from Account a WHERE a.login = :login AND a.password = :password")
.setParameter("login", login).setParameter("password", password);
manager.getTransaction().commit();
try {
account = (Account) query.getSingleResult(); // HERE Occures error
}
catch(NoResultException lackOfResult){
return false;
}
if(account != null)
{
return true;
}
return false;
}
}
答案 0 :(得分:1)
您需要添加
您的帐户类<{p>}上的@org.hibernate.annotations.DiscriminatorOptions(force=true)