josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Why is my ConstraintViolationException not being caught?

I'm using Play Framework, and have the following code:

import org.hibernate.exception.ConstraintViolationException;

...
public class AuthorService extends Controller

    ...

    public static void delete(Long id)  
    {
        Author author = Author.findById(id);    
        if(author == null) {
            renderError("Attempt to delete author has failed! The entry could not be found.", 500);
        }
        try {
            author.delete();
        } catch (ConstraintViolationException e) {
            renderError("A foreign key violation exception was thrown trying to delete <b>" + author.name + "</b>!", 500);
        }
        renderGSON(jobDescription);
    }

    ...
}

I'm not using cascading for deleting the record, as the foreign items have to be set to an entry selected by the user later on.

My problem is, that the ContraintViolationException is never caught, but my IDE reports it being thrown? Why is this? I want to catch it, deal with it and continue the application as normal.

Upvotes: 3

Views: 1424

Answers (2)

dr jerry
dr jerry

Reputation: 10026

I'm not familiar with play but constraintviolation exceptions may happen when your session is flushed (on an end of transaction, typical a submit).

Upvotes: 0

Pere Villega
Pere Villega

Reputation: 16439

Because in Play, as per code (delete calls _delete), that method only throws javax.persistence.PersistenceException and RuntimeException.

Upvotes: 2

Related Questions