PersistenceException: Cannot delete or update a parent row: a foreign key constraint fails

609 views
Skip to first unread message

Alvaro Denis Acosta Quesada

unread,
Jan 9, 2016, 4:48:42 PM1/9/16
to Ebean ORM

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) {
            .
            .
            .
        }
    }

Rob Bygrave

unread,
Jan 9, 2016, 6:13:00 PM1/9/16
to ebean@googlegroups

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.

Alvaro Denis Acosta Quesada

unread,
Jan 9, 2016, 6:58:57 PM1/9/16
to Ebean ORM
Thanks a lot Rob, good weekend.
Regards

Rob Bygrave

unread,
Jan 12, 2016, 8:39:10 PM1/12/16
to ebean@googlegroups
Logged as  #528 - ENH: Improve exception handling such that more specific types of exceptions can be detected such as constraint violation


--
Reply all
Reply to author
Forward
0 new messages