Java EE architecture - is DAO recommended to use ORM like JPA 2?

If I use ORM like JPA2 - where do I have objects that map to my database, should I use DAO? This seems a lot more overhead.

For example, I will need to support three additional packages:

  • One that defines the objects of my domain (which pretty much display my Entity objects):

    public class Employee { private String firstName; private String lastName; ... // Getters and setters } 
  • One that contains the interfaces that define my DAO methods

     public interface EmployeeDAO { public void addEmployee(Employee employee); public Employee getEmployeeById(long id); ... } 
  • One that contains a beans session that implements my DAO

     public EmployeeJpaDAO implements EmployeeDAO { interface method implementations here .... private method that transform my Employee entity into my Employee domain object } 

Now that a lot of excess baggage is added every time I need to perform a new CRUD operation.

However, the benefits that I see from having a DAO are:

  • You may have an in-memory DAO implementation for unit testing your level of service. This means that you do not need to access the database to verify the business logic, and you can be sure that your objects will always have the same property values.

  • Separates business logic from database access logic

An option that does not include a DAO implementation is to simply use the object and EntityManager objects in the service level:

 @Stateless public class EmployeeEjb { @PersistenceContext(unitName = "employee") private EntityManager manager; public Employee getEmployeeById(long id) { return manager.createNamedQuery(Employee.GetEmployeeById).getResultList(); } ... } 

Is there a middle ground here? Has anyone come across an architecture or implemented an architecture that satisfies some of the advantages of the DAO level (which is most important for the business logic under test), which I mentioned above, but does not include all the overhead associated with implementing the DAO level?

Thanks for any recommendations and / or suggestions! I am really interested to know what some people have come up with regarding this.

+45
java java-ee architecture dao
Sep 29 '10 at 3:52
source share
2 answers

If I use ORM like JPA2 - where do I have objects that map to my database, should I use DAO? This seems a lot more overhead.

It. And, obviously, Java EE does not encourage the use of the DAO template when using JPA (JPA already provides a standardized implementation of the Domain Store , and closing it behind the DAO is not that important. I believe that the DAO will be anti- DRY in this situation.

So, for simple cases (in fact, in most cases), I happily skipped the DAO, and I have no problem with that. For more complex cases (for example, when using stored procedures, flat files) I would use it. In other words, it depends on how summed up in the JPA really killed the DAO? . See also the following questions:

Related Questions

  • I found that JPA, or does not seem to encourage the DAO pattern
  • Simple but nice template for EJB
  • What is a good layer separation strategy for an application that can be used online and offline?
  • Using JSF, JPA and DAO. Without Spring?
  • What is the suitable DAO structure with jpa2 / eclipselink?

(...) The one that contains the beans session that implements my DAOs

Noooo, you certainly don’t want to implement DAO as a Bean session:

  • You do not want to create as many (concatenated) Bean sessions as tables (a big waste of resources)
  • You do not want the entire chain of beans sessions, do not reproduce errors from the past, this is a known bad practice that does not scale well.

So, if you really want to go the DAO path and want EM to be introduced, either implement your DAO as Spring beans (in Java EE 5) or managed CDI Bean (in Java EE 6),

You may have an in-memory DAO implementation for unit testing your level of service.

If you really want to test unit , mock DAO / EntityManager, there is no difference. And if you want to perform integration testing, you can configure JPA to use the database in memory. So in the end, I just don't buy this argument.

Separates business logic from database access logic

Honestly, I don’t see the big difference between relying on a DAO and an entity manager, I don’t see how DAO separate things are “better”. Again, I am not buying this argument.

And, in my experience, changing the basic persistence solution is a very exceptional event, and I am not going to introduce a DAO for something that most likely will not happen ( YAGNI , KISS ).

Is there a middle ground here? Has anyone come across an architecture or implemented an architecture that satisfies some of the advantages of the DAO level (which is most important for the business logic under test), which I mentioned above, but does not include all the overhead associated with implementing the DAO level?

I do not see much space and, as I hinted strongly, I do not use DAO if I do not feel the need. And, as I said, ridicule the EntityManager if you want a truly unit test of business logic. It works for me and I am happy to write less code.

Additional resources

+48
Sep 30 '10 at 12:35
source share

For the record.

For the principle of single responsibility (SRP), the DAO must have , it separates the model and logic at the level of persistence, which can be easily portable.

If the project uses a test module, the DAO helps to test it correctly (layout, database testing, etc.).

DAO is a SERVICE, and, like one, we can wrap our process in a class that is easy to service.

JPA reduces the number of lines of code, but what's more, the old rules still apply.

JPA adds a repository layer, but its not our repository . In the same principle, let me say that we encapsulated the logic that makes profit from some process. Encapsulation may be just a multiplication of 0.1, but it is still worthy of encapsulation.

For example, let's say that I have the following problem: for some odd reason, can I insert a new client into the database ?. Where should I start testing ?. a: ClientDao, if there is, if not, then good luck elsewhere.

+4
Mar 18 '15 at 18:37
source share



All Articles