2017-05-03 2 views
0

Dies ist die Methode, die die JPQL Instruktion ausführen wird und speichern sie in der Bohne Jointure1Probleme JPQL Ergebnis von zwei Entität kommen in einer 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; 
    } 

Dies ist die Art Jointure1; es ist nicht als eine Einheit erklärt und nicht in dem 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; 
} 

} 

Dies ist ein Verfahren in Form Paket deklariert, welche die Ticket-ID von der HTTP-Anforderung an den JPQL Befehlsparameter senden sammelt.

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

Dieses Servlet ist, (die den Fehler erzeugt):

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) 

Antwort

1

Die die JPQL Abfrage Zurückgeben einer Liste von Objekt [] , Ein Objekt [] für jede Zeile.

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