segque as classes Autor e AutorBean.
@Entity
public class Autor {
@Id
@GeneratedValue
private Integer id;
private String nome;
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 remove(Autor autor) {
new DAO<Autor>(Autor.class).remove(autor);
}
}
AutorBean
@ManagedBean
@ViewScoped
public class AutorBean implements Serializable {
private Autor autor = new Autor();
public Autor getAutor() {
return autor;
}
/*
* public void gravar() { System.out.println("Gravando autor " +
* this.autor.getNome());
*
* this.autor = new Autor();
*
* new DAO<Autor>(Autor.class).adiciona(this.autor); }
*/
public ForwardView gravar() {
System.out.println("Gravando autor " + this.autor.getNome());
if (this.autor.getId() == null) {
new DAO<Autor>(Autor.class).adiciona(this.autor);
this.autor = new Autor();
} else {
new DAO<Autor>(Autor.class).atualiza(this.autor);
}
return new ForwardView("livro");
}
public List<Autor> getAutores() {
return new DAO<Autor>(Autor.class).listaTodos();
}
public void carregar(Autor autor) {
this.autor = autor;
System.out.println("nome" + autor.getNome());
}
public void remover(Autor autor) {
this.autor.remove(autor);
}
}
ERROR: Cannot delete or update a parent row: a foreign key constraint fails (`livrariadb`.`livro_autor`, CONSTRAINT `FK9590997AD70C7E4A` FOREIGN KEY (`autores_id`) REFERENCES `autor` (`id`))
mar 20, 2017 4:18:08 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
ADVERTÊNCIA: #{autorBean.remover(autor)}: javax.persistence.RollbackException: Error while committing the transaction
javax.faces.FacesException: #{autorBean.remover(autor)}: javax.persistence.RollbackException: Error while committing the transaction
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIData.broadcast(UIData.java:1092)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:791)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1256)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)