Olá Pessoal,
Estou aprendendo JSF com JPA/Hibernate, fiz conforme um exemplo de uma apostila, mas
não consigo entender onde esta o erro, fico grato se alguém puder me ajudar
Meu arquivo persistence.xml
<?xml version="1.0" encoding = "UTF-8"?>
<persistence version = "2.0"
xmlns = "http://java.sun.com/xml/ns/persistence"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" >
<persistence-unit name ="biblioteca" transaction-type = "RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence </provider>
<properties>
<property name = "hibernate.dialect" value = "org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name = "hibernate.hbm2ddl.auto " value = "create" />
<property name = "javax.persistence.jdbc.driver" value = "com.mysql.jdbc.Driver " />
<property name = "javax.persistence.jdbc.user" value = "fabiano" />
<property name = "javax.persistence.jdbc.password" value = "150978" />
<property name = "javax.persistence.jdbc.url" value ="jdbc:mysql://localhost:3306/biblioteca"/>
</properties>
</persistence-unit>
</persistence>
Classe JPAFiltro
package br.com.biblioteca.filtro;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(servletNames={"Faces Servlet"})
public class JPAFiltro implements Filter {
private EntityManagerFactory fabrica;
@Override
public void destroy() {
this.fabrica.close();
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
EntityManager gerenciar = this.fabrica.createEntityManager();
arg0.setAttribute("EntityManager", gerenciar);
gerenciar.getTransaction().begin();
arg2.doFilter(arg0, arg1);
try{
gerenciar.getTransaction().commit();
}
catch(Exception e){
gerenciar.getTransaction().rollback();
}
finally{
gerenciar.close();
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
this.fabrica = Persistence.createEntityManagerFactory("biblioteca");
}
}
Minha classe bean Aluno
package br.com.biblioteca.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Aluno {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Column(name="nome")
private String nome;
@Column(name="rg")
private long rg;
@Column(name="telefone")
private String telefone;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public long getRg() {
return rg;
}
public void setRg(long rg) {
this.rg = rg;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
Classe Aluno Repositório
package br.com.biblioteca.beans;
import javax.persistence.EntityManager;
public class AlunoRepositorio {
private EntityManager gerenciar;
public AlunoRepositorio(EntityManager gerenciar) {
this.gerenciar = gerenciar;
}
public void adiciona(Aluno aluno){
this.gerenciar.persist(aluno);
}
}
Classe AlunoBeans
package br.com.biblioteca.managedbeans;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import br.com.biblioteca.beans.Aluno;
import br.com.biblioteca.beans.AlunoRepositorio;
@ManagedBean
public class AlunoBeans {
private Aluno aluno = new Aluno();
public void adicionaAluno(){
EntityManager gerencia = this.getEntityManager();
AlunoRepositorio repositorio = new AlunoRepositorio(gerencia);
repositorio.adiciona(this.aluno);
}
private EntityManager getEntityManager(){
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager gerencia = (EntityManager) request.getAttribute("EntityManager");
return gerencia;
}
public Aluno getAluno() {
return aluno;
}
public void setAluno(Aluno aluno) {
this.aluno = aluno;
}
}
Minha JSF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>SistemaBiblioteca-Aluno</title>
</h:head>
<h:body>
<h:form id="cadastrarAluno">
<h1>Cadastro de Aluno</h1>
<table>
<tr>
<td><h:outputLabel value="Aluno" /></td>
<td><h:inputText value="#{alunoBeans.aluno.nome}" required="true" id="nome" /></td>
</tr>
<tr>
<td><h:outputLabel value="RG" /></td>
<td><h:inputText value="#{alunoBeans.aluno.rg}" required="true" id="rg" /></td>
</tr>
<tr>
<td><h:outputLabel value="Telefone" /></td>
<td><h:inputText value="#{alunoBeans.aluno.telefone}" required="true" id="telefone" /></td>
</tr>
<tr>
<td></td>
<td><h:commandButton value="Cadastrar" action="#{alunoBeans.adicionaAluno()}"/></td>
</tr>
</table>
</h:form>
</h:body>
</html>
A mensagem de erro que apresenta
O erro apresenta na parte em negrito, mas não consigo fazer funcionar.
Mai 03, 2012 7:41:03 PM com.sun.faces.application.ActionListenerImpl processAction
Grave: java.lang.NullPointerException
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at br.com.biblioteca.beans.AlunoRepositorio.adiciona(AlunoRepositorio.java:15)
at br.com.biblioteca.managedbeans.AlunoBeans.adicionaAluno(AlunoBeans.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 24 more
Mai 03, 2012 7:41:03 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
Advertência: #{alunoBeans.adicionaAluno()}: java.lang.NullPointerException
javax.faces.FacesException: #{alunoBeans.adicionaAluno()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 23 more
Caused by: java.lang.NullPointerException
[b]at br.com.biblioteca.beans.AlunoRepositorio.adiciona(AlunoRepositorio.java:15)
at br.com.biblioteca.managedbeans.AlunoBeans.adicionaAluno(AlunoBeans.java:22)[/b]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 24 more