JPQL createQuery与查询返回类型Collection不兼容

时间:2015-03-27 14:39:11

标签: java jpa jpql createquery

我希望根据我的旅行(“tournee”)在Tournee(profil_tournee)列表中获取我的个人资料。但是,我有一个例外。任何人都可以帮助我吗?

  

线程“AWT-EventQueue-0”中的异常   java.lang.IllegalArgumentException:为TypedQuery指定的类型   [fr.galettedebroons.domain.Profil]与查询返回不兼容   type [interface java.util.Collection]

请求:

 List<List<Profil>> listProfil = Arrays.asList(manager_.createQuery("select t.profil_tournee "
            + "FROM Tournee t WHERE t.nom LIKE :tournee", Profil.class)
            .setParameter("tournee", tournee)
            .getResultList());

型号:

@Entity
public class Tournee {
    private int id;
    private String nom;
    private boolean lundi = false;
    private boolean mardi = false;
    private boolean mercredi = false;
    private boolean jeudi = false;
    private boolean vendredi = false;
    private boolean samedi = false;
    private boolean dimanche = false;
    private List<Profil> profil_tournee;

    public Tournee(){}

    public Tournee(String nom, boolean lundi, boolean mardi, boolean mercredi, boolean jeudi,
            boolean vendredi, boolean samedi, boolean dimanche, List<Profil> profil_tournee) {
        this.nom = nom;
        this.lundi = lundi;
        this.mardi = mardi;
        this.mercredi = mercredi;
        this.jeudi = jeudi;
        this.vendredi = vendredi;
        this.samedi = samedi;
        this.dimanche = dimanche;
        this.profil_tournee = profil_tournee;
    }

    public Tournee(String nom, boolean lundi, boolean mardi, boolean mercredi, boolean jeudi,
            boolean vendredi, boolean samedi, boolean dimanche) {
        this.nom = nom;
        this.lundi = lundi;
        this.mardi = mardi;
        this.mercredi = mercredi;
        this.jeudi = jeudi;
        this.vendredi = vendredi;
        this.samedi = samedi;
        this.dimanche = dimanche;
    }

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id_tournee) {
        this.id = id_tournee;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public boolean isLundi() {
        return lundi;
    }

    public void setLundi(boolean lundi) {
        this.lundi = lundi;
    }

    public boolean isMardi() {
        return mardi;
    }

    public void setMardi(boolean mardi) {
        this.mardi = mardi;
    }

    public boolean isMercredi() {
        return mercredi;
    }

    public void setMercredi(boolean mercredi) {
        this.mercredi = mercredi;
    }

    public boolean isJeudi() {
        return jeudi;
    }

    public void setJeudi(boolean jeudi) {
        this.jeudi = jeudi;
    }

    public boolean isVendredi() {
        return vendredi;
    }

    public void setVendredi(boolean vendredi) {
        this.vendredi = vendredi;
    }

    public boolean isSamedi() {
        return samedi;
    }

    public void setSamedi(boolean samedi) {
        this.samedi = samedi;
    }

    public boolean isDimanche() {
        return dimanche;
    }

    public void setDimanche(boolean dimanche) {
        this.dimanche = dimanche;
    }

    @OneToMany(mappedBy="profil_tournee", cascade=CascadeType.PERSIST)
    public List<Profil> getProfil_tournee() {
        return profil_tournee;
    }

    public void setProfil_tournee(List<Profil> profil_tournee) {
        this.profil_tournee = profil_tournee;
    }
}
@Entity
public class Profil {
    private String code_client;
    private Client client_profil;
    private Gamme gamme_profil;
    private List<Livraison> livraison_profil;
    private Boolean actif;
    private Tournee profil_tournee;
    private List<MargeLivraison> marge_profil;
    private List<Prevision> prevision_profil;

    public Profil(){}

    public Profil(Gamme code_gamme, List<Livraison> livraison, Boolean actif) {
        this.gamme_profil = code_gamme;
        this.livraison_profil = livraison;
        this.actif = actif;
    }

    @Id
    public String getCode_client() {
        return code_client;
    }

    public void setCode_client(String code_client) {
        this.code_client = code_client;
    }

    public Boolean getActif() {
        return actif;
    }

    public void setActif(Boolean actif) {
        this.actif = actif;
    }

    @ManyToOne
    public Gamme getGamme_profil() {
        return gamme_profil;
    }

    public void setGamme_profil(Gamme gamme_profil) {
        this.gamme_profil = gamme_profil;
    }

    @OneToMany(mappedBy="livraison_profil", cascade=CascadeType.PERSIST)
    public List<Livraison> getLivraison_profil() {
        return livraison_profil;
    }

    public void setLivraison_profil(List<Livraison> livraison_profil) {
        this.livraison_profil = livraison_profil;
    }

    @ManyToOne
    public Client getClient_profil() {
        return client_profil;
    }

    public void setClient_profil(Client client) {
        this.client_profil = client;
    }

    @ManyToOne
    public Tournee getProfil_tournee() {
        return profil_tournee;
    }

    public void setProfil_tournee(Tournee profil_tournee) {
        this.profil_tournee = profil_tournee;
    }

    @OneToMany(mappedBy="marge_profil", cascade=CascadeType.PERSIST)
    public List<MargeLivraison> getMarge_profil() {
        return marge_profil;
    }

    public void setMarge_profil(List<MargeLivraison> marge_profil) {
        this.marge_profil = marge_profil;
    }

    @OneToMany(mappedBy="prevision_profil", cascade=CascadeType.PERSIST)
    public List<Prevision> getPrevision_profil() {
        return prevision_profil;
    }

    public void setPrevision_profil(List<Prevision> prevision_profil) {
        this.prevision_profil = prevision_profil;
    }

2 个答案:

答案 0 :(得分:0)

您的预期结果列表将包含作为个人资料列表的元素,而不是个人资料。

我会用List.class替换Profil.class来创建查询:

List<List<Profil>> listProfil = Arrays.asList(manager_.createQuery("select t.profil_tournee "
        + "FROM Tournee t WHERE t.nom LIKE :tournee", List.class)
        .setParameter("tournee", tournee)
        .getResultList());

答案 1 :(得分:0)

您的错误提示您在调用EntityManager.createQuery(query, Type)方法时返回的类型应与声明的类型一致:

List<SomeType> em.createQuery("SELECT s FROM SomeType", SomeType.class);

然而,您真正的问题是您的查询是非法的。在JPA 中,集合值表达式不能成为SELECT子句的一部分。请参阅我的另一个答案https://stackoverflow.com/a/25890863/3796586

您的案例中的解决方案是颠倒查询,如下所示:

List<Profil> result = em.createQuery("SELECT p FROM Profil p WHERE" +
    "p.profil_tournee.norm LIKE :tournee", Profil.class)
        .setParameter("tournee", tournee)
        .getResultList());