mardi 16 juin 2015

Set Customized Validation Exception Message in JPA 2.0

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 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 the code reaches the catch(Exception e){} block without any specific validation exception message.

Is there a way to achieve what I want to do. If any further explanation is required please ask or any code snippet you guys want. Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire