Error when returning the object after query

Asked

Viewed 99 times

0

Good night to you all.

Objective: The main objective is to take the user id and the code of the simulated that he answered, and display it on another screen after he answers this simulated, however, I have Exception when trying to perform a query through my Dao and do not know how to solve.

Exception:

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sisEnade.tcc.modelo.Resposta
    at com.sisEnade.tcc.dao.ResultadoSimuladoDAO.resultadoRespostasSimulado(ResultadoSimuladoDAO.java:22)
    at com.sisEnade.tcc.controller.ResultadoSimuladoBean.init(ResultadoSimuladoBean.java:37)



GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/sisEnade] threw exception [WELD-000049: Unable to invoke public void com.sisEnade.tcc.controller.ResultadoSimuladoBean.init() on com.sisEnade.tcc.controller.ResultadoSimuladoBean@74e35f8d] with root cause
    ... 91 more

DAO:

@Inject
    private EntityManager manager;


    // Métodos de buscas para resultado do aluno e futuramente relatórios..

    public Resposta resultadoRespostasSimulado(Long codigoUsuario, Long codigoSimulado) {
        Resposta resposta =  (Resposta) manager.createQuery
                ("select u.nome, SUM(r.respostasAcertadas) as Respostas_Corretas from Resposta r "
                 + "JOIN r.usuario u where r.usuario.codigo = ?1 AND r.simulado.codigo = ?2")
                .setParameter(1, codigoUsuario)
                .setParameter(2, codigoSimulado)
                .getSingleResult();

        return resposta;
    }

The bean that is involved with this DAO is as follows:

@Inject
    private ResultadoSimuladoDAO resultadoSimuladoDAO;

    private Resposta resultado;

    @PostConstruct
    public void init() {
        FacesContext fUsuario = FacesContext.getCurrentInstance();
        HttpSession sessionUsuario = (HttpSession) fUsuario.getExternalContext().getSession(false);
        Long codigoUsuario = (Long) sessionUsuario.getAttribute("identificaUsuario");

        FacesContext fSimulado = FacesContext.getCurrentInstance();
        HttpSession sessionSimulado = (HttpSession) fSimulado.getExternalContext().getSession(false);
        Long codigoSimulado = (Long) sessionSimulado.getAttribute("identificaSimulado");

        resultado = this.resultadoSimuladoDAO.resultadoRespostasSimulado(
                codigoUsuario, codigoSimulado);

    }

    public Resposta getResultado() {
        return resultado;
    }
  • What’s the class like Resposta?

  • This is why the query does not return an object of type Resposta, and yes another with two attributes, name and Respostas_Corretas. Try something like select new Resposta(u.nome, SUM(r.respostasAcertadas)) ... and inform the class in the query as well, something like manager.createQuery("a query aqui", Resposta.class)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.