Handling EJB/JPA exceptions in a “beautiful” way

Posted by Rodrigues, Raphael on Oracle Blogs See other posts from Oracle Blogs or by Rodrigues, Raphael
Published on Thu, 6 Sep 2012 19:39:17 +0000 Indexed on 2012/09/06 21:46 UTC
Read the original article Hit count: 731

Filed under:

In order to handle JPA exceptions, there are some ways already detailed in lots of blogs. Here, I intend to show one of them, which I consider kind of “beauty”.

My use case has a unique constraint, when the User try to create a duplicate value in database. The JPA throws a java.sql.SQLIntegrityConstraintViolationException, and I have to catch it and replace the message.

In fact, ADF Business Components framework already has a beautiful solution for this very well documented here . However, for EJB/JPA there's no similar approach.

In my case, what I had to do was:

1. Create a custom Error Handler Class in DataBindings file;

a. Here is how you accomplish it.

2. Override the reportException method and check if the type of exception exists on property file

a. If yes, I change the message and rethrows the exception

b. If not, go on the execution

The main goal of this approach is whether a new or unhandled Exception was raised, the job is, only create a single entry in bundle property file.

Here are pictures step by step:

1. CustomExceptionHandler.java

2. Databindings.cpx

3. Bundle file

4. jspx:

5. Stacktrace:

Give your opinion, what did you think about that?

© Oracle Blogs or respective owner

Related posts about /ADF