Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Removing a detached instance

843830Oct 14 2008 — edited Oct 14 2008
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?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2008
Added on Oct 14 2008
4 comments
1,666 views