TheNorth
TheNorth

Reputation: 57

trouble Collecting JPQL result of two entity join in a list

This is the method that will execute the JPQL instruction and store it in the bean Jointure1

private static final String PARAM_TICKET           = "id_ticket";
private static final String JPQL_SELECT ="SELECT u.nom, u.prenom, r.texte, r.date_post FROM Utilisateur u, ReponseTicket r WHERE u.idemp = r.id_employe AND r.id_ticket=:id_ticket";

public List<Jointure1> trouverJointure( int id_ticket ) throws DAOException {
        List<Jointure1> liste;
        TypedQuery<Jointure1> query = em.createQuery(JPQL_SELECT, Jointure1.class);
        query.setParameter(PARAM_TICKET, id_ticket);
        try {
            liste = (List<Jointure1>) query.getResultList();
        } catch ( NoResultException e ) {
            return null;
        } catch(Exception e) {
            throw new DAOException(e);
        }
        return liste;
    }

This is the type Jointure1; it is not declared as an entity and is not declared in the persistence.xml

//TEXTE - DATE_POST - NOM  - PRENOM - AGENCE - POSTE - DEPARTEMENT - ID_EMPLOYE
public class Jointure1 {
private String texte;
private String nom;
private String prenom;
private String Agence;
private String poste;
private String departement;
private int id_employe;
private Timestamp date_post;
public String getTexte() {
    return texte;
}
public void setTexte(String texte) {
    this.texte = texte;
}
public String getNom() {
    return nom;
}
public void setNom(String nom) {
    this.nom = nom;
}
public String getPrenom() {
    return prenom;
}
public void setPrenom(String prenom) {
    this.prenom = prenom;
}
public String getAgence() {
    return Agence;
}
public void setAgence(String agence) {
    Agence = agence;
}
public String getPoste() {
    return poste;
}
public void setPoste(String poste) {
    this.poste = poste;
}
public String getDepartement() {
    return departement;
}
public void setDepartement(String departement) {
    this.departement = departement;
}
public int getId_employe() {
    return id_employe;
}
public void setId_employe(int id_employe) {
    this.id_employe = id_employe;
}
public Timestamp getDate_post() {
    return date_post;
}
public void setDate_post(Timestamp date_post) {
    this.date_post = date_post;
}

}

This is a method in the form package, which collects the ticket id from the HTTP request to send it to the JPQL instruction parameter.

public List<Jointure1> recupererJointure(HttpServletRequest request)
{
    List<Jointure1> ljointure;
    int id = getId_ticket(request);
    if(id!=0){
        ljointure = reponseDao.trouverJointure(id);
    }else ljointure=null;
    return ljointure;
}

This is servlet, ( which generates the error ):

CreationReponseForm reponse = new CreationReponseForm(reponseDao);
    List<Jointure1> listereponse = reponse.recupererJointure(request);

    if(ticket==null)
    {
    response.sendRedirect("/connexion");
    } else {
        System.out.println("CECI EST UN NOM:"+listereponse.get(0).getNom()); // << Error here this is the line 46
        request.setAttribute("lreponse", listereponse);
        request.setAttribute("ticket", ticket);
        this.getServletContext().getRequestDispatcher("/WEB-INF/ReponsesTickets.jsp").forward(request, response);
    }
}

Stacktrace:

    [2017-05-03T17:02:03.886+0100] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=30 _ThreadName=http-listener-1(4)] [timeMillis: 1493827323886] [levelValue: 900] [[
  StandardWrapperValve[projet.helpdesk.servlets.Reponsestickets]: Servlet.service() for servlet projet.helpdesk.servlets.Reponsestickets threw exception
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to projet.helpdesk.beans.Jointure1
    at projet.helpdesk.servlets.Reponsestickets.doPost(Reponsestickets.java:46)
    at projet.helpdesk.servlets.Reponsestickets.doGet(Reponsestickets.java:31)

Upvotes: 0

Views: 35

Answers (1)

TheNorth
TheNorth

Reputation: 57

The the JPQL query is returning a list of Object[], One Object[] for each row.

public List<Object[]> trouverJointure( int id_ticket ) throws DAOException {
            List<Object[]> liste;
            TypedQuery<Object[]> query = em.createQuery(JPQL_SELECT, Object[].class);
            query.setParameter(PARAM_TICKET, id_ticket);
            try {
                liste = (List<Object[]>) query.getResultList();
            } catch ( NoResultException e ) {
                return null;
            } catch(Exception e) {
                throw new DAOException(e);
            }
            return liste;
        }

Upvotes: 1

Related Questions