简体   繁体   English

我应该在EJB 3.1,JSF2.0和JPA中使用Bean Validation吗?

[英]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. 我有一个JSF 2.0应用程序,我想开始添加验证器。 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. 我有Managed Backing Beans,它包含我的JPA注释类和EJB的实例。 The EJB's are responsible for accessing the database transactionally. EJB负责以事务方式访问数据库。 The JPA annotated classes are valuebound to my facelets (and use the EJB's for db access). JPA带注释的类是我的facelets的值绑定(并使用EJB进行数据库访问)。

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. 我想潜在地使用Bean验证并编写自定义约束,但这意味着我必须将这些约束添加到我的JPA注释类中。 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? (将演示文稿/验证与JPA注释类/ DAO混合)在这种情况下不使用Bean验证会更好吗? Is my structure flawed? 我的结构有缺陷吗? Is there a preferred way of doing this that I do not know about? 有没有一种我不了解的首选方式?

Thanks! 谢谢!

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. 因此,在JPA实体中使用它们绝对没有错。

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. 验证不属于演示文稿,因为即使您更改了表示层(假设您要在EJB上公开一堆Web服务),验证仍然需要存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM