E ae galera blza? É o seguinte: Estou tentando fazer uns testes usando JSF + Hibernate e estou apanhando rsrs… Utilizando o Hibernate XML eu consegui persistir os dados e na hora de listar os dados deu erro. Mas não é esse problema que quero resolver neste momento.
Agora estou usando Annotations e nessa nem cadastrar eu estou conseguindo. Abaixo estão os códigos que estou usando neste exemplo e logo depois o erro que está sendo apresentado:
Obs: Estou usando o Netbeans 6.1 / testei com o eclipse e o erro é o mesmo.
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/aluno</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<mapping class="entidades.Aluno" />
</session-factory>
</hibernate-configuration>
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<!-- O Bean Aluno -->
<managed-bean>
<managed-bean-name>alunoBean</managed-bean-name>
<managed-bean-class>controle.AlunoBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>forwardToJSF.jsp</welcome-file>
</welcome-file-list>
</web-app>
messages.properties
operacaoSucesso = Operação Concluída com Sucesso.
operacaoFracasso = Operação não Concluída.
util.HibernateUtil
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static{
Configuration conf = new AnnotationConfiguration();
conf.configure();
factory = conf.buildSessionFactory();
}
public static Session getSession(){
return factory.openSession();
}
}
util.SessionUtil
package util;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class SessionUtil {
private static ResourceBundle bundle = ResourceBundle.getBundle("message",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
public static void addErrorMessage(String msg){
msg = bundle.getString(msg);
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage(null, facesMsg);
}
public static void addSuccessMessage(String msg){
msg = bundle.getString(msg);
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage("successInfo", facesMsg);
}
}
entidades.Aluno
package entidades;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Aluno {
@Id
@GeneratedValue
private int id;
private String nome;
private int idade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
}
controle.AlunoBean
package controle;
import entidades.Aluno;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;
import util.SessionUtil;
public class AlunoBean {
private Aluno aluno = new Aluno();
public Aluno getAluno() {
return aluno;
}
public void setAluno(Aluno aluno) {
this.aluno = aluno;
}
public String salvar(){
//cria a sessao com o banco e a transacao...
Session session = HibernateUtil.getSession();
Transaction t = session.beginTransaction();
try{
//se tudo der certo, salva no banco de dados
session.merge(aluno);
t.commit();
SessionUtil.addSuccessMessage("operacaoSucesso");
//limpa formulario aluno
Aluno aluno = new Aluno();
}catch(Exception e){
t.rollback();
SessionUtil.addErrorMessage("operacaoFracasso");
}finally{
session.close();
}
return null;
}
}
[b]Páginas web
cadastroAluno.jsp[/b]
<%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Page</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="3">
<h:outputText value="Nome" />
<h:inputText id="nome" value="#{alunoBean.aluno.nome}" />
<h:message for="nome" />
<h:outputText value="Idade" />
<h:inputText id="idade" value="#{alunoBean.aluno.idade}" />
<h:message for="idade" />
</h:panelGrid>
<h:commandButton value="Salvar" action="#{alunoBean.salvar}" />
</h:form>
</f:view>
</body>
</html>
forwardToJSF.jsp
<jsp:forward page="cadastroAluno.jsf"/>
Aí estão os arquivos com os quais estou trabalhando. E logo abaixo o erro:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: #{alunoBean.salvar}: java.lang.NoClassDefFoundError: org/hibernate/loader/custom/SQLQueryReturn
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
javax.faces.FacesException: #{alunoBean.salvar}: java.lang.NoClassDefFoundError: org/hibernate/loader/custom/SQLQueryReturn
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:107)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
javax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: org/hibernate/loader/custom/SQLQueryReturn
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.NoClassDefFoundError: org/hibernate/loader/custom/SQLQueryReturn
org.hibernate.cfg.annotations.QueryBinder.bindSqlResultsetMapping(QueryBinder.java:293)
org.hibernate.cfg.AnnotationBinder.bindQueries(AnnotationBinder.java:238)
org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:403)
org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:353)
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:265)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
util.HibernateUtil.(HibernateUtil.java:14)
controle.AlunoBean.salvar(AlunoBean.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.el.parser.AstValue.invoke(AstValue.java:152)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
javax.faces.component.UICommand.broadcast(UICommand.java:383)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Bom, fico aguardando ajuda. Preciso fazer um sistema Diário Virtual para apresentar no TCC em janeiro e estou correndo contra o tempo. Abraço a todos e desde já agradeço.