使用布尔函数进行选择

时间:2016-05-04 20:50:37

标签: java hibernate many-to-many jpql

我正在尝试使用Java EE开发应用程序。以下是我想要实现的类图的一部分:

enter image description here

以下是使用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]

3 个答案:

答案 0 :(得分:1)

您忘记了:来表明您的命名参数,因此您使用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

答案 1 :(得分:1)

Module实体中,您将modulePk定义为主键。使用此ID有两种可能性:

  • 创建ModulePk类的实例并将其传递给实体管理器的find()方法(在查询中不是这种情况),或者

  • 要在JPQL查询中使用它,您必须遍历嵌入式id类。在您的情况下使用它如下:

    String jpql = "select case when (count(m) > 0)  then true else false end  from Module m where m.modulePk.idEnseignant = :idEnseignant and m.modulePk.idClasse = :idClasse";
    

答案 2 :(得分:0)

@jens

@Entity
@Table(name = "t_module")
public class Module implements Serializable {
private static final long serialVersionUID = 1L;

private ModulePk modulePk;
private String nomModule;
private int nbreHeure;
private Enseignant enseignant;
private Classe classe;

public Module() {
}

public Module(String nomModule, int nbreHeure, Enseignant enseignant, Classe classe) {
    this.nomModule = nomModule;
    this.nbreHeure = nbreHeure;
    this.enseignant = enseignant;
    this.classe = classe;
    this.modulePk = new ModulePk(enseignant.getId(), classe.getId());
}

@EmbeddedId
public ModulePk getModulePk() {
    return modulePk;
}

public void setModulePk(ModulePk modulePk) {
    this.modulePk = modulePk;
}

public String getNomModule() {
    return nomModule;
}

public void setNomModule(String nomModule) {
    this.nomModule = nomModule;
}

public int getNbreHeure() {
    return nbreHeure;
}

public void setNbreHeure(int nbreHeure) {
    this.nbreHeure = nbreHeure;
}

@ManyToOne
@JoinColumn(name = "idEnseignant", referencedColumnName = "id", insertable = false, updatable = false)
public Enseignant getEnseignant() {
    return enseignant;
}

public void setEnseignant(Enseignant enseignant) {
    this.enseignant = enseignant;
}

@ManyToOne
@JoinColumn(name = "idClasse", referencedColumnName = "id", insertable = false, updatable = false)
public Classe getClasse() {
    return classe;
}

public void setClasse(Classe classe) {
    this.classe = classe;
}

}

这是ModulePK类

@Embeddable
public class ModulePk implements Serializable {

private static final long serialVersionUID = 1L;

private int idEnseignant;
private int idClasse;

public ModulePk() {
}

public ModulePk(int idEnseignant, int idClasse) {
    this.setIdEnseignant(idEnseignant);
    this.setIdClasse(idClasse);
}
...