JPA CDI DAO Injection into Object

I am new to JPA and CDI, and I am trying to create an enterprise application using these frameworks.

I get how I can stick in beans and keep everything in order and stateless. I also understand that JPA loads relationships, etc. For me, so I no longer need to worry about it. I still use my DAO for specific search methods and, of course, to create new objects.

I understand that I do not want to inject material into my objects, since they are controlled by JPA, and I need to use the new keyword to create a new object (instead of loading).

I use to manage my objects with other classes, for example, if we have a User and a group, I use the idle bean status to manage the group (create new, search, etc.), and this idle beans uses my DAO to retrieve and sending data.

I am using a Group object to manage users (maybe this is wrong?), But I do not want to introduce a DAO into the group, as this is an entity. I know that something is wrong with this design, but I cannot find the best practice for this.

Should all management classes be EJB? I'm used to creating domain classes for my logic, should I drop this concept, put all my logic in EJB and use objects to store data only?

+2
source share
1 answer

I use the Group object to manage users (maybe this is wrong?), But I do not want to enter the DAO into the group, as this is an entity. I have something wrong with this design, but I cannot find a better practice for this.

If a Group has User s, match it as a collection (possibly OneToMany).

Use another great bean to encapsulate save operations, for example. a GroupService or GroupDao . In this bean, you will enter the EntityManger, which is responsible for saving ("manage users and groups").

This tutorial should give you a start.

Should all management classes be EJB?

Of course, not necessarily. But it is a little difficult (read: impossible) to tell without knowing your requirements. I suggest you add separate questions with additional information, and then it’s easier to discuss your problem ...

Typically: try to separate objects (Group, User) from business logic and save operations (GroupService, ... Dao).

I find this book provides an excellent overview and discussion of the post J2EE template.

+1
source

Source: https://habr.com/ru/post/1435993/


All Articles