Depois de implementar o novo componente deu esse erro id to load is required for loading
.
Minha classe Autor:
package br.com.caelum.livraria.modelo;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import br.com.caelum.livraria.dao.DAO;
@Entity
@ViewScoped
public class Autor implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String nome;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void remover(Autor autor) {
new DAO<Autor>(Autor.class).remove(autor);
}
}
Classe AutorBean:
package br.com.caelum.livraria.bean;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import br.com.caelum.livraria.dao.DAO;
import br.com.caelum.livraria.modelo.Autor;
@ManagedBean
public class AutorBean {
private Autor autor = new Autor();
private Integer autorId;
public Integer getAutorId() {
return autorId;
}
public void setAutorId(Integer autorId) {
this.autorId = autorId;
}
public Autor getAutor() {
return autor;
}
public List<Autor> getAutores() {
return new DAO<Autor>(Autor.class).listaTodos();
}
public void removerAutor(Autor autor) {
try {
this.autor.remover(autor);
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("autor",
new FacesMessage(autor.getNome() + " contem livros associados a ele!"));
System.out.println("Tentando remover autor!");
}
}
// METODO carregarAutorPeloId
public void carregarAutorPeloId() {
this.autor = new DAO<Autor>(Autor.class).buscaPorId(autorId);
}
// METODO gravar autor
public void gravarAutor() {
if (this.autor.getId() == null) {
new DAO<Autor>(Autor.class).adiciona(this.autor);
} else {
new DAO<Autor>(Autor.class).atualiza(this.autor);
}
System.out.println("Livro do autor " + autor.getNome());
}
// METODO carrega autores na tela
public void carregaAutor(Autor autor) {
this.autor = autor;
}
public RedirectView gravar() {
System.out.println("Gravando autor " + this.autor.getNome());
new DAO<Autor>(Autor.class).adiciona(this.autor);
this.autor = new Autor();
return new RedirectView("livro");
}
}
Xhtml autor:
<?xml version="1.0" encoding="UTF-8" ?>
<!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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/_template.xhtml">
<f:metadata>
<f:viewParam name="autorId" value="#{autorBean.autorId}" />
<f:viewAction action="#{autorBean.carregarAutorPeloId}"
if="#{param != null}" />
</f:metadata>
<ui:define name="titulo">
Novo Autor
</ui:define>
<ui:define name="conteudo">
<h:form id="autor">
<h:messages id="messages" />
<fieldset>
<legend>Dados do Autor</legend>
<h:panelGrid columns="2">
<h:outputLabel value="Nome:" for="nome" />
<h:inputText id="nome" value="#{autorBean.autor.nome}">
<f:ajax event="blur" render="messages" />
</h:inputText>
<h:outputLabel value="Email:" for="email" />
<h:inputText value="#{autorBean.autor.email}" id="email">
<f:passThroughAttribute name="type" value="email" />
</h:inputText>
<h:commandButton value="Gravar" action="#{autorBean.gravarAutor}" />
</h:panelGrid>
</fieldset>
<h:dataTable value="#{autorBean.autores}" var="autor"
id="tabelaAutores">
<h:column>
<h:outputText value="#{autor.nome}" />
</h:column>
<h:column>
<h:commandLink value="alterar"
action="#{autorBean.carregaAutor(autor)}" />
</h:column>
<h:column>
<h:commandLink value="remover"
action="#{autorBean.removerAutor(autor)}" />
</h:column>
</h:dataTable>
</h:form>
</ui:define>
<ui:define name="texto">Cadastro de Autores</ui:define>
</ui:composition>
</html>