我应该将 Bean 验证与 EJB 3.1、JSF2.0 和 JPA 一起使用吗?

发布于 2024-08-23 16:50:04 字数 320 浏览 4 评论 0原文

我有一个 JSF 2.0 应用程序,我想开始为其添加验证器。该应用程序架构的基本概述如下。

我有托管支持 Bean,其中包含 JPA 注释类和 EJB 的实例。 EJB 负责以事务方式访问数据库。 JPA 带注释的类与我的 Facelet 进行值绑定(并使用 EJB 进行数据库访问)。

我希望可能使用 Bean 验证并编写自定义约束,但这意味着我必须将这些约束添加到我的 JPA 带注释的类中。在我看来,这违反了我的关注点分离。 (将表示/验证与 JPA 带注释的类/DAO 混合)在这种情况下最好不使用 Bean 验证吗?我的结构有缺陷吗?有没有我不知道的首选方法?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2024-08-30 16:50:04

验证是业务逻辑的一部分,因此必须存在于作为持久性实体的域模型中。因此,将它们放在 JPA 实体中绝对没有问题。

验证不属于表示层,因为即使您更改表示层(假设您要在 EJB 上公开一堆 Web 服务),验证仍然需要存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文