Criei um projeto de gestão pedagógica e ao usar o método de remoção ele retorna um erro java.lang.IllegalArgumentException: attempt to create delete event with null entity.
Os métodos em si (na minha opnião) não estão com o código errado o erro provavelmente está na jsp.
Postarei aqui os códigos.
Controller
package br.com.tcc.controllers;
import java.util.List;
import javax.inject.Inject;
import javax.validation.Valid;
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.IncludeParameters;
import br.com.caelum.vraptor.validator.Validator;
import br.com.tcc.dao.TurmaDao;
import br.com.tcc.modelos.Turma;
@Controller
public class TurmaController {
private Result result;
private Validator validator;
private TurmaDao turmaDao;
@Inject
public TurmaController(Result result, Validator validator, TurmaDao turmaDao) {
this.result = result;
this.validator = validator;
this.turmaDao = turmaDao;
}
@Deprecated
public TurmaController(){
}
public void form(){
}
@IncludeParameters
public void adiciona(@Valid Turma turma){
validator.onErrorForwardTo(this).form();
turmaDao.adiciona(turma);
result.redirectTo(this).lista();
}
public void lista(){
List<Turma> turmas = turmaDao.lista();
result.include("turmas", turmas);
}
@IncludeParameters @Post
public void remove(Turma turma){
System.out.println("Id da turma" + turma.getId());
turmaDao.remove(turma);
result.redirectTo(this).lista();
}
public void formRemocao(){
result.include("turmas", turmaDao.lista());
}
}
Dao:
package br.com.tcc.dao;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import br.com.tcc.modelos.Turma;
public class TurmaDao {
private EntityManager manager;
@Inject
public TurmaDao(EntityManager manager) {
this.manager = manager;
}
@Deprecated
public TurmaDao(){
}
public void adiciona(Turma turma){
manager.getTransaction().begin();
manager.persist(turma);
manager.getTransaction().commit();
}
public Turma busca(Turma turma) {
return manager.find(Turma.class, turma.getId());
}
public List<Turma> lista(){
TypedQuery<Turma> query = manager.createQuery("select t from Turma as t",Turma.class);
return query.getResultList();
}
public void remove(Turma turma) {
manager.getTransaction().begin();
turma = busca(turma);
manager.remove(turma);
manager.getTransaction().commit();
}
public void altera(Turma turma){
manager.getTransaction().begin();
Turma turmaNova = busca(turma);
turmaNova.setAno(turma.getAno());
turmaNova.setEtapa(turma.getEtapa());
turmaNova.setNome(turma.getNome());
turmaNova.setSerie(turma.getSerie());
manager.getTransaction().commit();
}
}
JSPS:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="alura"%>
<c:import url="/WEB-INF/jsp/header.jsp"></c:import>
<form action="${linkTo[TurmaController].remove(null)}" method="post">
<label for="turmas">Selecione uma turma:</label>
<select id="turmas" name="turma">
<c:forEach var="turma" items="${turmas}">
<option value="${turma.id}">"${turma.nome}"</option>
</c:forEach>
</select> <input type="submit" value="Remover" class="btn" />
</form>
<c:import url="/WEB-INF/jsp/footer.jsp"></c:import>
Na stacktrace reparei que o id não está passando corretamente:
mai 11, 2017 6:20:07 PM org.hibernate.validator.internal.xml.ValidationXmlParser unmarshal
INFO: HV000007: META-INF/validation.xml found. Parsing XML based configuration.
Id da turma 0
Hibernate:
select
turma0_.id as id1_7_0_,
turma0_.ano as ano2_7_0_,
turma0_.etapa as etapa3_7_0_,
turma0_.nome as nome4_7_0_,
turma0_.serie as serie5_7_0_
from
Turma turma0_
where
turma0_.id=?
mai 11, 2017 6:20:07 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [default] in context with path [/pedagogico] threw exception [java.lang.IllegalArgumentException: attempt to create delete event with null entity] with root cause
java.lang.IllegalArgumentException: attempt to create delete event with null entity