persistence@glassfish.java.net

AW: Re: Entities become unmanaged after RollbackException

From: Christian Reiter <christian_at_percom.at>
Date: Wed, 7 May 2008 15:51:57 +0200

Hi James!

Thanks for your fast answer!

In the meanwhile i found a solution to avoid the problem:

try {
     Country deleteCandidate = em.find(Country.class, c.getCountryRowid());
     t.begin();
     em.remove(deleteCandidate);
     t.commit();
     listCountries.remove(c);
} catch (Exception e) {
     ... Error Handling ...
}

Does this solution contain any drawbacks?

Is there a chance that this very dissatisfying Error Handling gets any better
in future versions of JPA? Or is there any other way to use Foreign Key Constraints?
 
Mit freundlichen Grüßen / Kind Regards

                                        Christian Reiter
                                        PerCom-Soft HandelsgesmbH

------->> NEU:
Direkte Erreichbarkeit über ENUM und SIP:
ENUM: +43 5242 61177 23
SIP: <sip:52426117723_at_voice.percom.at>
<<-------


--
Christian Reiter
christian@percom.at || http://www.percom.at
fon: +43 5242 61177 || mob: +43 664 924 55 27 || fax: +43 5242 61177 31
 
> -----Ursprüngliche Nachricht-----
> Von: James Sutherland [mailto:jamesssss_at_yahoo.com] 
> Gesendet: Mittwoch, 7. Mai 2008 15:26
> An: persistence_at_glassfish.dev.java.net
> Betreff: Re: Entities become unmanaged after RollbackException
> 
> 
> 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-Rollback
> Exception-tp17100308p17104834.html
> Sent from the java.net - glassfish persistence mailing list 
> archive at Nabble.com.
> 
> 
>