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.
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. > >
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. >> >>--
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. > >> > >> > >
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. >> >> >> >> >> >>--