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