I have an issue in my code, this are working but is very ugly and error prone, I am checking error conditions based on the Exception message, so I need a recommendation to change this. I have an entity A and an entity B that have a B(identifier) reference as "foreign key", so if found a B that reference an A and I try to delete A the operation fail and launch "PersistenceException".
NOTES:
1 - I am not interested in a delete in cascade.
2 - I am not interested in preconditions guarantees(checking that no found one B that reference A before delete A).
3 - My goal is try to remove A, if fail then process the Exception, but
replacing the pe.toString().contains("...") by an enum, error type or other option(if this is possible).
Thanks in advance, this is my code:
public static Result delete() {
JsonNode json = request().body().asJson();
try {
Long id = json.get("id").asLong();
models.EntityA entity = models.EntityA.findByPropertie("id", id);
models.EntityA.delete(entity);
return ok();
} catch(PersistenceException pe) {
if (pe.toString().contains("Cannot delete or update a parent row: a foreign key constraint fails")) {
appLogger.error(Messages.get("EntityA.delete.ForeignKeyConstraint"), pe);
return internalServerError("ForeignKeyConstraint");
} else {
appLogger.error(Messages.get("EntityA.delete.unexpectedError"), pe);
return internalServerError("unexpectedError");
}
} catch (Exception e) {
.
.
.
}
}Yes I understand. We would like to be able to identify more specific classes of exception such as "constraint violation".
Currently with Ebean we don't have a nice way of doing this and it would be good to do this.
The JPA exceptions don't currently give us a standard way to expose this so there is some thinking/design required (exposing SQL state code and/or interpretation into DB agnostic error classes such as constraint violation).
... A useful problem to solve, I'll try to look at this more this coming week.
--
---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--