persistence@glassfish.java.net

Re: Entities become unmanaged after RollbackException

From: James Sutherland <jamesssss_at_yahoo.com>
Date: Wed, 7 May 2008 06:25:33 -0700 (PDT)

Unfortunately JPA does not allow for any error handling or retry, after a
rollback the EntityManager is invalid and can no longer be used, all
Entities become detached. You need to create a new EntityManager and either
re-read or merge any objects into it.


Christian Reiter wrote:
>
> Hi!
>
> I'm having a problem when a RollbackException occures after trying to
> remove
> an Entity which is not allowed to be removed because of an Foreign Key
> Restrict Constraint.
>
> In Detail:
> - I've got two Tables in my DB: Postalcode and Country
> - Postalcode references to Country by a Foreign Key Constraint
> - It shouldn't be possible to remove a Country which is used in a
> Postalcode row
>
> When i try to remove a Country entity which isn't used in Postalcode
> everything works
> as expected.
>
> When i try to remove a Country which is used in Postalcode i get an
> RollbackException,
> this is also what i want and expect.
>
> Now the Problem:
> After trying to remove an Referenced Entity and handling the
> RollbackException i am trying
> to remove an unreferenced Entity - but then i get the following Exception:
>
> java.lang.IllegalArgumentException: Entity must be managed to call remove:
> Afg (FAG), try merging the detached and try the remove again.
>
> Removing unreferenced Entities prior to removing referenced Entities works
> fine. This happens
> only after trying to remove an referenced Entity.
>
> Why do the Entities get unmanaged?
> I've already tried to do a merge() before deleting, but this results in
> the same error.
>
> Here's what i am doing to remove the Entity:
>
> EntityTransaction t = em.getTransaction();
> try {
> t.begin();
> em.remove(c);
> t.commit();
> listCountries.remove(c);
> } catch (RollbackException e) {
> JOptionPane.showMessageDialog(
> panelCountries,
> "Country already referenced in other Table"
> );
> }
>
>
> Mit freundlichen Grüßen / Kind Regards
>
> Christian Reiter
> PerCom-Soft HandelsgesmbH
>
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Entities-become-unmanaged-after-RollbackException-tp17100308p17104834.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.