Boa dia pessoal, estou com probleminha na minha aplicação jsf, criei um crud na aplicação deu certo para inserir e excluir, mas na hora de editar tive um problema a aplicação só altera o registro que acabei de inserir, os registros que inseri anteriormente não consigo alterar.
conforme vocês poderão ver no meu código:
classe Pergunta.
package br.gera.dominio;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="pergunta")
@Entity
public class Pergunta implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String descricao;
private String alternativa;
private String resposta;
public Pergunta(){
}
public Pergunta( long id, String descr, String alterna, String resp){
this.id = id;
this.descricao = descr;
this.alternativa = alterna;
this.resposta =resp;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getAlternativa() {
return alternativa;
}
public void setAlternativa(String alternativa) {
this.alternativa = alternativa;
}
public String getResposta() {
return resposta;
}
public void setResposta(String resposta) {
this.resposta = resposta;
}
}
interface PerguntaDao.
package br.gera.Dao;
import java.util.List;
import br.gera.dominio.Pergunta;
public interface PerguntaDao {
List<Pergunta> buscarTodos();
// List<Pergunta> buscarPorId();
void salvar(Pergunta pergunta);
void remover(Pergunta pergunta);
void Update(Pergunta pergunta);
}
classe PerguntaDaoImpl
package br.gera.Dao.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.gera.Dao.PerguntaDao;
import br.gera.dominio.Pergunta;
import br.gera.util.HibernateUtil;
public class PerguntaDaoImpl implements PerguntaDao {
/*
* (non-Javadoc)
*
* @see br.gera.Dao.PerguntaDao#buscarTodos()
*/
@SuppressWarnings("unchecked")
public List<Pergunta> buscarTodos() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
List<Pergunta> lista = session.createQuery("from Pergunta").list();
t.commit();
return lista;
}
// @SuppressWarnings("unchecked")
// public List<Pergunta> buscarPorId() {
// Session session = HibernateUtil.getSessionFactory().openSession();
// return (List<Pergunta>) session.load(Pergunta.class, session);
//
// }
public void salvar(Pergunta pergunta) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
session.save(pergunta);
t.commit();
session.clear();
session.close();
}
public void remover(Pergunta pergunta) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
session.delete(pergunta);
t.commit();
}
public void Update(Pergunta pergunta){
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
session.update(pergunta);
session.flush();
t.commit();
session.clear();
session.close();
}
}
PerguntaMBean
package br.gera.Mbean;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import br.gera.Dao.PerguntaDao;
import br.gera.Dao.impl.PerguntaDaoImpl;
import br.gera.dominio.Pergunta;
@ManagedBean(name = "perguntaMBean")
@SessionScoped
public class PerguntaMBean {
private Pergunta pergunta;
private DataModel<Pergunta> perguntas;
public DataModel<Pergunta> getListarPegunta() {
List<Pergunta> lista = new PerguntaDaoImpl().buscarTodos();
perguntas = new ListDataModel<Pergunta>(lista);
return perguntas;
}
public Pergunta getPergunta() {
if(pergunta == null)
pergunta = new Pergunta();
return pergunta;
}
public void setPergunta(Pergunta pergunta) {
this.pergunta = pergunta;
}
public String prepararAddPergunta() {
pergunta = new Pergunta();
return "form";
}
public String prepararAlterarPergunta() {
pergunta = (Pergunta) (perguntas.getRowData());
return "form";
}
public String Excluir() {
Pergunta perguntemp = (Pergunta) (perguntas.getRowData());
PerguntaDao dao = new PerguntaDaoImpl();
dao.remover(perguntemp);
return "form";
}
public String Salvar() {
PerguntaDao dao = new PerguntaDaoImpl();
dao.salvar(pergunta);
return "index";
}
public String Alterar() {
PerguntaDao dao = new PerguntaDaoImpl();
dao.Update(pergunta);
return "form";
}
}
form
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:form >
Descrição: <h:inputText id="descricao"
value="#{ perguntaMBean.pergunta.descricao }" />
<br />
Alternativa: <h:inputText id="alternativa"
value="#{ perguntaMBean.pergunta.alternativa }" />
<br />
Resposta: <h:inputText id="resposta"
value="#{ perguntaMBean.pergunta.resposta }" />
<br />
<h:inputHidden value="#{ perguntaMBean.pergunta.id }" />
<h:commandButton value="Salvar" action="#{ perguntaMBean.Salvar() }" />
<a href="lista.jsf">Listar</a>
</h:form>
</html>
lista
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<a href="form.jsf">Cadastrar nova pergunta</a>
<br />
<h:form>
<h:dataTable rows="20" border="2" value="#{ perguntaMBean.listarPegunta}" var="e" style="width : 744px; height : 52px;">
<h:column>
<f:facet name="header">id</f:facet>
<h:outputText value="#{ e.id }" />
</h:column>
<h:column>
<f:facet name="header">descriçao</f:facet>
<h:outputText value="#{ e.descricao }" />
</h:column>
<h:column>
<f:facet name="header">alternativa</f:facet>
<h:outputText value="#{ e.alternativa }" />
</h:column>
<h:column>
<f:facet name="header">resposta</f:facet>
<h:outputText value="#{ e.resposta }" />
</h:column>
<h:column>
<h:commandButton value="Editar" action="#{ perguntaMBean.Alterar() }" />
</h:column>
<h:column>
<h:commandButton value="Remover" action="#{ perguntaMBean.Excluir() }" />
</h:column>
</h:dataTable>
</h:form>
</html>
edita
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:form >
Descrição: <h:inputText id="descricao"
value="#{ perguntaMBean.pergunta.descricao }" />
<br />
Alternativa: <h:inputText id="alternativa"
value="#{ perguntaMBean.pergunta.alternativa }" />
<br />
Resposta: <h:inputText id="resposta"
value="#{ perguntaMBean.pergunta.resposta }" />
<br />
<h:inputHidden value="#{ perguntaMBean.pergunta.id }" />
<h:commandButton value="Alterar" action="#{ perguntaMBean.Alterar() }" />
<a href="lista.jsf">Listar</a>
</h:form>
</html>
E quando eu rodo somente a lista para lista os elementos do banco e mando editar exibe esse erro abaixo:
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: java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
root cause
javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIData.broadcast(UIData.java:912)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
root cause
java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:40)
org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:23)
org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)
br.gera.Dao.impl.PerguntaDaoImpl.Update(PerguntaDaoImpl.java:58)
br.gera.Mbean.PerguntaMBean.Alterar(PerguntaMBean.java:71)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:264)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIData.broadcast(UIData.java:912)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.27