openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pawel Veselov <pawel.vese...@gmail.com>
Subject Re: Handle constraint violation
Date Fri, 20 Jul 2012 19:01:05 GMT
On Thu, Jul 19, 2012 at 3:05 PM, José Luis Cetina <maxtorzito@gmail.com>wrote:

> And what about if my object has references in 100 tables? do i have to
> do 100 SELECT'S??
>
> Is no a more "flexible" way to do it??
>

I wouldn't call it flexible, and I wouldn't personally recommend doing it
in other way but finding out using some query whether your object is
referenced or not.

With JTA, you can create a new transaction, for the purpose of making this
test. Exactly the way you were doing it. If the transaction fails, roll
back and consider that your object is being referenced.

You can also use save points, but then you'll need to access underlying
Connection object. The idea would be to commit to a save point, attempt the
delete, flush, and then rollback to savepoint if it failed.




> 2012/7/19 Pawel Veselov <pawel.veselov@gmail.com>:
> > Hi.
> >
> > On Thu, Jul 19, 2012 at 2:56 PM, José Luis Cetina <maxtorzito@gmail.com
> >wrote:
> >
> >> Hi im triying to handle a constraint violation but i dont know how..
> >>
> >> In my EJB i have this:
> >>
> >> try {
> >>             UserTransaction userTransaction =
> >> ejbContext.getUserTransaction();
> >>             userTransaction.begin();
> >>             em.remove(anyObject);
> >>             userTransaction.commit();
> >>      }
> >> catch(Exception ex){
> >>    //WHAT DO I HAVE TO DO HERE, FOR HANDLE THE CONSTRAINT VIOLATION???
> >> }
> >>
> >> The exception class is:  javax.transaction.RollbackException
> >>
> >> [skipped]
> >
> >>
> >>
> >> I know this exception is because im triying to delete a child before
> >> delete his parent, what i want is to check if my object has a
> >> reference in other table (i dont want to use a SELECT for each table)
> >> that's why i want to handle a constraint violation (can be only the:
> >> Cannot delete or update a parent row: a foreign key constraint fails.)
> >> how can i do this?
> >>
> >
> > You should do the select. The database will effectively do a select
> anyway,
> > to do the constraint check, you are not introducing much overhead.
> >
> > There are certain databases, and certain drivers, that will not tolerate
> > any data exception inside a transaction, and will mark the whole
> > transaction as "roll back only" should any data exception occur.
> >
> > [skipped]
> >
> >
> >> Im using JTA and OpenJPA 2.2.0
> >>
> >> Thanks.
> >>
> >>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message