Set Customized Validation Exception Message in JPA 2.0

126 Views Asked by At

I am working on a Web Service JaxRs project where I get all the DB scripts from DBA. So first I execute that DB script to create all the tables and all the constraints on the tables attributes, in the DB itself(DB2 here) like character length etc. Then in the project I use Generate Entities from Table in Eclipse which creates all the model classes.

But the main problem arises here.There is a table "USER" having an attribute username which is nullable false and userType which is max 3 characters long. These restrictions/validations/constraints are there in the DB itself but not in the Java User Model Class.Also i cannot change those Model classes and add these validations here in code in model classes.

Now while creating the user I am putting the logic for these constraints in DAO layer while saving. But instead of the prior one, I want to have mechanism for error message that a constraints has failed while saving like if the input userType provided by front-end is more than 3 char which fails the validation, using i18n.properties file or any api/framework.

Currently if any validation fails while calling EntityManager persist() and then commit() method the code reaches the last catch(RollbackException e){} block without any specific validation exception message OR any cause of rollback exception.

Is there a way to achieve what I want to do. How can I generalize this so that it can be applied to my model class & checks for the constraints in DB using JNDI lookup and give customised messages. If any further explanation is required please ask or any code snippet you want.

0

There are 0 best solutions below