I have created my first jpa web application, all goes well but now I have to implement a remove operation on a mapped object. But when I call EntityManager.remove on it, I get this excetpion:
java.lang.IllegalArgumentException: Entity must be managed to call remove: Abba Pino, try merging the detached and try the remove again.
the guilty code is this:
private void eliminaCliente(Evento evento) {
Cliente cliente = this.finestraCliente.getCliente();
ClienteFacade facade = new ClienteFacade();
try {
facade.getEm().getTransaction().begin();
facade.getEm().merge(cliente);
facade.getEm().remove(cliente);
facade.getEm().flush();
} catch(PersistenceException e) {
String messaggio;
if(e.getCause() instanceof ConstraintViolationException) { // vincolo di integrità violato
messaggio = "Impossibile completare l'operazione a causa della violazione di un vincolo di integrità:\n---> ";
messaggio += e.getCause().getCause().getMessage();
} else // errore generico
messaggio = e.getMessage();
apriFinestraMessaggio(evento, messaggio);
} finally {
facade.getEm().close();
}
}
as you can see, I call the merge method on my detached entity
before calling remove. Why doesn't it work?