2017-05-03 5 views
0

Ceci est la méthode qui exécutera l'instruction JPQL et la stocker dans la fève Jointure1la difficulté à toucher résultat JPQL de deux entités se joindre à une liste

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

C'est le type Jointure1; il n'est pas déclarée comme une entité et non déclarée dans les 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; 
} 

} 

Ceci est une méthode dans le package de forme, qui recueille l'identifiant de billets de la requête HTTP pour l'envoyer au paramètre d'instruction JPQL.

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

Ceci est servlet, (qui génère l'erreur):

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) 

Répondre

1

La la requête JPQL retourne une liste d'objet [] , Un objet [] pour chaque ligne.

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