Reputation: 317
I've got an Account
abstract class which is inherited by two other classes:
DoctorAccount
, NurseAccount
(i omited getters and setters in order to keep post brief). After performing the method checkIfLoginAndPasswordIsCorrect
i get an error:
Caused by: org.hibernate.WrongClassException: Object [id=1] was not of the specified subclass [Model.Account] : Discriminator: 1
. I found out that query.getSingleResult();
is responsible for this, but I dont know why it throws such errors. Using polymorphism I can assign Account to Doctor/Nurse. Do You have any idea what i'm doing wrong?
@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{
}
In logInDAO class:
@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;
}
}
Upvotes: 1
Views: 1038
Reputation: 3205
You need to add
@org.hibernate.annotations.DiscriminatorOptions(force=true)
on your Account class
Upvotes: 2