ArchiveOrangemail archive

users.openjpa.apache.org


(List home) (Recent threads) (2 other Apache OpenJPA lists)

Subscription Options

  • RSS or Atom: Read-only subscription using a browser or aggregator. This is the recommended way if you don't need to send messages to the list. You can learn more about feed syndication and clients here.
  • Conventional: All messages are delivered to your mail address, and you can reply. To subscribe, send an email to the list's subscribe address with "subscribe" in the subject line, or visit the list's homepage here.
  • Low traffic list: less than 3 messages per day
  • This list contains about 10,776 messages, beginning Jun 2007
  • 7 messages added yesterday
Report the Spam
This button sends a spam report to the moderator. Please use it sparingly. For other removal requests, read this.
Are you sure? yes no

Handle constraint violation

José Luis Cetina 1342734995Thu, 19 Jul 2012 21:56:35 +0000 (UTC)
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

Im getting this:

openjpa-2.2.0-r422266:1244990 fatal store error>
org.apache.openjpa.persistence.EntityExistsException: The transaction
has been rolled back.  See the nested exceptions for details on the
errors that occurred.

Caused by: <openjpa-2.2.0-r422266:1244990 fatal store error>
org.apache.openjpa.persistence.EntityExistsException: Cannot delete or
update a parent row: a foreign key constraint...

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: Cannot
delete or update a parent row: a foreign key constraint fails.........
[code=1451, state=23000]


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?

If i iterate over the ex.getCause() i only get one.

 Throwable cause = ex.getCause();
 while(cause!=null)
   cause = cause.getCause();

Im using JTA and OpenJPA 2.2.0

Thanks.-------------------------------------------------------------------
SCJA. José Luis Cetina
Pawel Veselov 1342735370Thu, 19 Jul 2012 22:02:50 +0000 (UTC)
Hi.On Thu, Jul 19, 2012 at 2:56 PM, José Luis Cetina 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.
>
>
José Luis Cetina 1342735547Thu, 19 Jul 2012 22:05:47 +0000 (UTC)
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??2012/7/19 Pawel Veselov :
> Hi.
>
> On Thu, Jul 19, 2012 at 2:56 PM, José Luis Cetina 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.
>>
>>---------------------------------------------------------------------
SCJA. José Luis Cetina
Pawel Veselov 1342810915Fri, 20 Jul 2012 19:01:55 +0000 (UTC)
On Thu, Jul 19, 2012 at 3:05 PM, José Luis Cetina 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 :
> > Hi.
> >
> > On Thu, Jul 19, 2012 at 2:56 PM, José Luis Cetina 
> >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.
> >>
> >>
>
>
José Luis Cetina 1342814014Fri, 20 Jul 2012 19:53:34 +0000 (UTC)
Im creating a method for doing this "dyanimic", the method read the
annotations and looking from relationships and check (with select), i
have 80% and is working without problems.2012/7/20 Pawel Veselov :
> On Thu, Jul 19, 2012 at 3:05 PM, José Luis Cetina 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 :
>> > Hi.
>> >
>> > On Thu, Jul 19, 2012 at 2:56 PM, José Luis Cetina 
>> >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.
>> >>
>> >>
>>
>>---------------------------------------------------------------------
SCJA. José Luis Cetina
Home | About | Privacy