@christianMoura escreveu:
Boa noite, estou recebendo o seguinte erro em tempo de execução (ClassCastException)
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to pojo.ClientePojo
Alinha em que ocorre o erro é um system.out: >> System.out.println(lista.get(i).getNome());
Minha classe Pojo simplificada:
import enums.TipoCliente;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;/**
*
* @author SV EXPRESS
*/@Entity
@Table(name = "clientes")
public class ClientePojo implements Serializable {@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) @Column(name = "ID") private long idCliente; @Column(name = "NOME_CLIENTE") private String nome; @Column(name = "SENHA") private String senha; @Enumerated(EnumType.STRING) @Column(name = "TIPO_CLIENTE") private TipoCliente tipoCliente;
public long getIdCliente() {
return idCliente;
}/** * @return the nome */ public String getNome() { return nome; } /** * @param nome the nome to set */ public void setNome(String nome) { this.nome = nome; } public TipoCliente getTipoCliente() { return tipoCliente; } /** * @param tipoCliente the tipoCliente to set */ public void setTipoCliente(TipoCliente tipoCliente) { this.tipoCliente = tipoCliente; }
meu método na classe DAO:
public List procurarCliente(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("select cp.nome, cp.tipoCliente from ClientePojo cp"); List lista = query.getResultList(); session.getTransaction().commit(); HibernateUtil.closeSession(); return lista; }
Onde chamo o método:
List lista = new ArrayList<>();
lista.addAll(cdao.procurarCliente(id));for (int i = 0; i < lista.size(); i++) {
System.out.println(lista.get(i).getNome());
}
Minha dúvida: gostaria de saber o porque de ele tentar dar cast,e de que forma poderia resolver isso. Creio que poderia fazer com o uso de Generics mas não domino o assunto
obs: Consigo exibir o valor do List caso a hql que eu execute seja "from ClientePojo", mas não acho legal fazer isso pois retorna todas as colunas do banco, e isso em grande escala não seria legal.
Mensagens: 1
Participantes: 1