0

我想根据我的旅行(“tournee”)在 Tournee 类(profil_tournee)列表中获取我的个人资料。但是,我有一个例外。谁能帮我?

线程“AWT-EventQueue-0”java.lang.IllegalArgumentException 中的异常:为 TypedQuery [fr.galettedebroons.domain.Profil] 指定的类型与查询返回类型 [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;
    }
4

2 回答 2

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());
于 2015-03-28T12:25:38.000 回答
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());
于 2015-03-27T14:56:09.200 回答