我正在尝试使用Java EE开发应用程序。这是我要实现的类图的一部分:
这是hibernate类的实现:
@Entity
@Table(name = "t_enseignant")
public class Enseignant extends User implements Serializable {
private static final long serialVersionUID = 1L;
private String specialite;
private List<Module> modules;
public Enseignant() {
}
public Enseignant(String nom, String prenom, String email, String login, String password, String specialite) {
super(nom, prenom, email, login, password);
this.setSpecialite(specialite);
}
public String getSpecialite() {
return specialite;
}
public void setSpecialite(String specialite) {
this.specialite = specialite;
}
@OneToMany(mappedBy = "enseignant")
public List<Module> getModules() {
return modules;
}
public void setModules(List<Module> modules) {
this.modules = modules;
}
}
@Entity
@Table(name = "t_classe")
public class Classe implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String niveau;
private int nbreEtudiant;
private List<Module> modules;
public Classe() {
}
public Classe(String niveau, int nbreEtudiant) {
this.niveau = niveau;
this.nbreEtudiant = nbreEtudiant;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
...
我还实现了Module Class和ModulePK。
我想检查一下是否应该一次指派一位老师上课。
我尝试了以下JPQL查询
boolean exists = false;
String jpql = "select case when (count(m) > 0) then true else false end from Module m where m.idEnseignant=idEnseignant and m.idClasse=idClasse";
TypedQuery<Boolean> query = entityManager.createQuery(jpql, Boolean.class);
query.setParameter("idEnseignant", enseignant.getId());
query.setParameter("idClasse", classeToValidate.getId());
exists = query.getSingleResult();
但是当我执行该方法时,我有这个异常
could not resolve property: idEnseignant of: domain.Module [select case when (count(m) > 0) then true else false end from domain.Module m where m.idEnseignant=idEnseignant and m.idClasse=idClasse]
最佳答案
您忘记了:
来指示您的命名参数,因此您使用的是idEnseignant=idEnseignant
而不是idEnseignant=:idEnseignant
这是更正的代码:
boolean exists = false;
String jpql = "select case when (count(m) > 0) then true else false end from Module m where m.idEnseignant=:idEnseignant and m.idClasse=:idClasse";
TypedQuery<Boolean> query = entityManager.createQuery(jpql, Boolean.class);
query.setParameter("idEnseignant", enseignant.getId());
query.setParameter("idClasse", classeToValidate.getId());
exists = query.getSingleResult();
return exists