JPQL createQuery is incompatible with the type of query return

advertisements

I want to get my profile in class Tournee (profil_tournee) list based on my tours ("tournee"). However, I have an exception. Can anyone help me?

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Type specified for TypedQuery [fr.galettedebroons.domain.Profil] is incompatible with query return type [interface java.util.Collection]

Request:

 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());

Model :

@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;
    }


Your expected result list will contain elements that are list of profiles, not profiles.

I would replace Profil.class by List.class for the Query creation :

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());