使用ManyToMany进行JPQL查询

时间:2016-03-16 16:18:01

标签: java jpa

我试图在JPQL之后执行此操作:

@Query("select o from Offre o where o.typeContrat.titre=:a or o.ville.nomVille=:b or o.competences.libelleCompetance = :c")
    public Page<Offre> offresSearch(@Param("a") String type, @Param("b") String location, @Param("c") String competence,Pageable pageable);

这是我的Offre实体:

@Entity
public class Offre implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CODE_OFFRE")
    private Long codeOffre;
    private String titre;
    @Lob
    private String description;
    @ManyToOne
    @JoinColumn(name="codeContratType")
    private TypeContrat typeContrat;

    @ManyToMany
    @JoinTable(name="OFFRE_COMP",
            joinColumns=@JoinColumn(name="CODE_OFFRE"),
            inverseJoinColumns=@JoinColumn(name="CODE_COMPETENCE"))
    private List<Competence> competences;

    // Getters and setters

这是我的能力实体:

    @Entity
    public class Competence implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "CODE_COMPETENCE")
    private Long codeCompetence;
    private String libelleCompetance;

    @ManyToMany(mappedBy="competences")
    private Collection<Offre> offres;

    // Getters, Setters

    @JsonIgnore
    @XmlTransient
    public Collection<Offre> getOffres() {
        return offres;
    }
    @JsonSetter
    public void setOffres(Collection<Offre> offres) {
        this.offres = offres;
    }
}

但是当我运行我的应用程序时,我收到了这个错误:

  

java.lang.IllegalArgumentException:org.hibernate.QueryException:   非法企图取消引用收集   [offre0_.code_offre.competences]包含元素属性引用   [libelleCompetance] [从com.capValue.Rekrute.entities.Offre中选择o   o其中o.typeContrat.titre =:a或o.ville.nomVille =:b或   o.competences.libelleCompetance =:c] at   org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)   ...

1 个答案:

答案 0 :(得分:1)

您无法在ToMany关系的查询中导航。你必须使用JOIN:

select o from Offre o join o.competences c 
where o.typeContrat.titre = :a or o.ville.nomVille = :b or c.libelleCompetance = :c