在列表中收集两个实体连接的JPQL结果

时间:2017-05-03 16:50:36

标签: jpa servlets java-ee casting jpql

这是执行JPQL指令并将其存储在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;
    }

这是Jointure1的类型;它未声明为实体,并且未在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;
}

}

这是表单包中的一个方法,它从HTTP请求中收集票证ID以将其发送到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;
}

这是servlet,(生成错误):

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

堆栈跟踪:

    [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)

1 个答案:

答案 0 :(得分:1)

JPQL查询返回每行的Object [],One Object []列表。

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