Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1928504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:59:12+00:00 2026-05-17T06:59:12+00:00

If I’m using an ORM like JPA2 – where I have my entities that

  • 0

If I’m using an ORM like JPA2 – where I have my entities that are mapped to my database, should I still be using a DAO? It seems like a lot more overhead.

For example, I would need to maintain three extra packages:

  1. One that specifies my domain objects (which pretty much map my Entity objects):

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

    public interface EmployeeDAO {
        public void addEmployee(Employee employee);
        public Employee getEmployeeById(long id);
        ...
    }
    
  3. One that contains session beans that implement my DAO’s

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

Now that’s a lot of extra baggage to add every time I need to perform a new CRUD operation.

However the benefits I see from having a DAO is:

  1. You can have an in memory implementation of the DAO for unit testing your service layer. This means you don’t need to access the database to test business logic, and you can be assured that your objects will always contain the same values for properties

  2. It separates business logic from database access logic

The option that doesn’t involve implementing a DAO is to just use entity objects and EntityManager in the service layer:

@Stateless
public class EmployeeEjb {
    @PersistenceContext(unitName = "employee")
    private EntityManager manager;

    public Employee getEmployeeById(long id) {
        return manager.createNamedQuery(Employee.GetEmployeeById).getResultList();
    }
    ...
}

Is there no middle ground here? Has anyone come across an architecture or implemented an architecture that meets some of the benefits of a DAO layer (most importantly the unit testability of business logic) that I mentioned above, but doesn’t involve all the overhead involved to implement a DAO layer?

Thanks for any recommendations and/or suggestions! I’m really curious to see what some people have come up with in regards to this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:59:12+00:00Added an answer on May 17, 2026 at 6:59 am

    If I’m using an ORM like JPA2 – where I have my entities that are mapped to my database, should I still be using a DAO? It seems like a lot more overhead.

    It is. And clearly, Java EE doesn’t encourage using the DAO pattern when using JPA (JPA already provides a standardized implementation of the Domain Store pattern and there isn’t much value at shielding it behind a DAO). I find the DAO to be anti-DRY in such situation.

    So for simple cases (actually, most cases), I happily skip the DAO and I have no problem with that. For more complex cases (for example when using stored procedures, flat files), I’d use it. In other words, it depends, as summarized in Has JPA Killed the DAO?. See also the related questions below:

    Related questions

    • I found JPA, or alike, don’t encourage DAO pattern
    • Simple but good pattern for EJB
    • What is a good strategy for seprating layers for an appication that can be used online and offline?
    • Using JSF, JPA and DAO. Without Spring?
    • What’s an appropriate DAO structure with jpa2/eclipselink?

    (…) One that contains session beans that implement my DAO’s

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

    • You don’t want to create as much (pooled) Session Bean as tables (big waste of resources)
    • You don’t want to chain Session Beans everywhere, don’t reproduce errors from the past, this is a known bad practice that doesn’t scale well.

    So if you really want to go the DAO way and want the EM to be injected, either implement your DAOs as Spring beans (in Java EE 5) or CDI managed bean (in Java EE 6).

    You can have an in memory implementation of the DAO for unit testing your service layer.

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

    It separates business logic from database access logic

    Honestly, I don’t see a big difference between relying on a DAO vs an entity manager, I don’t see how a DAO separate things “better”. Again, I don’t buy this argument.

    And to my experience, changing the underlying persistence solution is a very exceptional event and I’m not going to introduce DAOs for something that is very likely not going to happen (YAGNI, KISS).

    Is there no middle ground here? Has anyone come across an architecture or implemented an architecture that meets some of the benefits of a DAO layer (most importantly the unit testability of business logic) that I mentioned above, but doesn’t involve all the overhead involved to implement a DAO layer?

    I don’t see much middle ground and, as strongly hinted, I don’t use DAOs if I don’t feel the need. And as I said, mock the EntityManager if you want to truly unit test the business logic. It works for me and I’m happy to write less code.

    More resources

    • JPA/EJB3 killed the DAO
    • DAOs Aren’t Dead – But They Either Collapsed Or Disappeared
    • …And Finally, You Should Introduce A DAO If:
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.