Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Bean Validation with EJB 3.1, JSF2.0 and JPA?

I have a JSF 2.0 application that I would like to start adding validators for. A basic overview of the architecture of the app is as follows.

I have Managed Backing Beans that contain instances of my JPA annotated classes and EJB's. The EJB's are responsible for accessing the database transactionally. The JPA annotated classes are valuebound to my facelets (and use the EJB's for db access).

I would like to potentially use Bean validation and write custom constraints, however this will mean I have to add those constraints to my JPA annotated classes. This seems to me like it is violating my separation of concerns. (Mixing presentation/validation with the JPA annotated classes/DAOs) Is it better to not use Bean validation in this case? Is my structure flawed? Is there a preferred way of doing this that I do not know about?

Thanks!

like image 413
kgrad Avatar asked Mar 04 '10 03:03

kgrad


1 Answers

Validation is part of your business logic and hence must be present in your domain model which are your persistence entities. So there is absolutely nothing wrong in having them within your JPA entities.

Validations don't belong to presentation because even if you change your presentation layer (say you're gonna expose a bunch of web services on your EJBs), the validation still needs to be there.

like image 136
Chandra Sekar Avatar answered Nov 08 '22 18:11

Chandra Sekar