Como Consultar um dados na hibernate

2 respostas
jsnpereira

Pessoal!

Estou tentado consultando para buscar um dados para informar por exemplo para editar, pegar um dados e aparece os dados na tela para alterar alguns dados e depois salveOrUpdate. mas apareceu alguns erro que foram testando System.out.println, não funcionou pegar o dado. veja ai em baixo.

Mensagem de erro

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to Persistencia.Perfil
	Controlador.EditarServlet.doGet(EditarServlet.java:39)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.30 logs.


--------------------------------------------------------------------------------

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		
		if(cmd.equals("preditar")){
			
			int id = Integer.parseInt(request.getParameter("id"));
			
			SessionFactory sf = (new AnnotationConfiguration().configure("/cfg/hibernate.cfg.xml")
					.buildSessionFactory());
			Session sessao = sf.openSession();
			
			SQLQuery questao = sessao.createSQLQuery("select * from perfil where id="+id);
			
			List<Perfil> lista = questao.list();
			
			System.out.println("Nome: "+lista.get(0).getNome());
			
			sessao.close();
			
		} else if(cmd.equals("editar")){
		    // esse para salvar a editar que foram alteração do dados	
		}

Espero que vcs me ajudem… obrigado!

2 Respostas

jamirdeajr

Tenta adicionar a entidade na query, assim ele retorna objetos do tipo ‘Perfil’ e não array de Objects de cada campo:

jsnpereira

[quote=jamirdeajr]Tenta adicionar a entidade na query, assim ele retorna objetos do tipo ‘Perfil’ e não array de Objects de cada campo:

Acabei testar a programar e funcionou perfeitamente… muito obrigado!

Criado 22 de abril de 2011
Ultima resposta 25 de abr. de 2011
Respostas 2
Participantes 2