JPA CDI将DAO注入实体 [英] JPA CDI Injecting DAO into an Entity

查看:115
本文介绍了JPA CDI将DAO注入实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA和CDI的新手,我正在尝试使用这些框架创建一个企业应用程序。

I'm new to JPA and CDI and I'm trying to create an enterprise application using these frameworks.

我得到如何注入豆子,保持一切整洁和无状态。我也得到JPA加载关系等等,所以我不用再担心了。我仍然使用我的DAO的具体的查找方法和交流创建新的实体。

I get how I can inject into beans and keep everything tidy and stateless. I also get that JPA loads relations etc. for me so that I don't have to worry about it anymore. I still use my DAO's for specific find methods and ofcourse to create new entities.

我明白,我不想把东西注入我的实体,因为他们是由JPA管理,我需要使用新的关键字创建一个新的实体(而不是加载)。

I understand that I don't want to be injecting stuff into my entities since they're managed by JPA and I need to use the new keyword to create a new entity (instead of loading).

我习惯于用其他类管理我的实体,例如,如果我们有一个用户和一个组,我使用无状态bean来管理组(创建新的,找到等等),这个无状态的bean使用我的DAO来检索和发送数据。

I'm used to managing my entities with other classes, for example if we have a User and a Group I use a stateless bean to manage the group (create new ones, find ones etc) and this stateless beans uses my DAO to retrieve and send the data.

我使用组实体来管理用户(也许这是错误的?)但我不想将DAO注入到组中,因为它是一个实体。我知道这种设计有问题,但我找不到最佳实践。

I use the Group entity to manage the users (maybe this is wrong?) but I don't want to inject the DAO into the Group since it's an entity. I know there's something wrong in this design but I can't find the best practice for this.

所有管理类都应该是EJB吗?我习惯于为我的逻辑创建域类,我应该把这个概念放在远方,把我所有的逻辑放在EJB中,并使用Entities来保存数据?

Should all management classes be EJBs? I'm used to creating Domain classes for my logic, should I throw this concept away, put all my logic in EJBs and use the Entities for holding data only?

推荐答案


我使用组实体来管理用户(也许这是错误的)但是
我不想将DAO注入到组中它是一个实体。 I
知道这个设计有问题,但是我找不到最好的
的做法。

I use the Group entity to manage the users (maybe this is wrong?) but I don't want to inject the DAO into the Group since it's an entity. I know there's something wrong in this design but I can't find the best practice for this.

如果具有用户,则将其映射为集合(可能为OneToMany)。

If a Group has Users, map this as a collection (possibly OneToMany).

使用另一个不同的bean来封装持久化操作,例如一个 GroupService 或一个 GroupDao 。在这个bean中,您将注入一个负责持久化的EntityManger(管理用户和组)。

Use another distinct bean to encapsulate persistence operations, e.g. a GroupService or a GroupDao. In this bean you will inject an EntityManger, which is responsible for persistence ("manage Users and Groups").

本教程应该给您一个开始。


所有管理类都应该是EJB吗?

Should all management classes be EJBs?

当然不一定。但是,在不知道你的要求的情况下,它有点困难(见:不可能)。我建议您添加更多信息的孤立问题,然后讨论您的问题更容易...

Certainly not necessarily. But it's a bit hard (read: impossible) to tell without knowing your requirements. I suggest that you add isolated questions with more information, then it's way easier to discuss your problem...

作为经验法则:尝试分离实体(组,用户)从业务逻辑和持久化操作(GroupService,... Dao)。

As a rule of thumb: Try separating entities (Group, User) from business logic and persistence operations (GroupService, ...Dao).

我发现 这本书 给出了关于后J2EE模式的很好的概述和讨论。

I find this book gives an excellent overview and discussion about post J2EE pattern.

这篇关于JPA CDI将DAO注入实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆