BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Has JPA Killed the DAO?

Has JPA Killed the DAO?

This item in japanese

Bookmarks

Recently there has been some discussion as to whether or not the Java Persistence API (JPA) has killed the Data Access Object (DAO). JPA (Java Persistence API) defines an interface to persist normal Java objects (or POJO's in some peoples terminology) to a datastore. Essentially this provides object-relational mapping similar to Hibernate and others. On the other hand, a DAO can be summed up as,

A Data Access Object (DAO) is a software component that provides a common interface between the application and one or more data storage devices, such as a database or file.

 

As with most discussions of this nature, bloggers from both sides have taken their stance. On one side, which initiated this discussion, Adam Bien states,

...The usage cannot be simpler. The EntityManager will be just injected to the bean-class...The DAO pattern is actually no more interesting for general data access, but is still needed to access data from stored procedures, flat files etc...
Days later, magle argued that the DAO wasn't going anywhere,
there were some statements and blog posts about the end of the DAO pattern in the last weeks, especially in conjunction with the rising of EJB 3 and it's EntityManager. those proposals suggest to use EntityManager directly inside your business oriented services instead of encapsulating data access logic through a DAO. i strongly disagree with this opinion...
Magle goes on to explain himself through the rest of the article by highlighting on various key issues: In a follow up post to JPA/EJB3 Killed the DAO, Adam Bien continues to spell out his reasoning for why the DAO is no longer necessary,
The DAO-Pattern can be considered as a "Data Service Layer", which encapsulates the particular and often proprietary data access realization. The main purpose of such a layer would be to make your independent of the particular database or OR-mapper. But even in the past I never replaced the database or even switched from SQL to LDAP.
There are some cases, like e.g. encapsulation of legacy systems, were layers are mandatory. DAOs in my opinion can be highly optimized in Java EE 5 (until it disappears :-)). The DAO-Interface, implementation and Factory and the actual Session Bean can be collapsed. Of course we could argue, that it isn't very good to be dependent on EJB 3 - but why not? Just because of @Stateless annotation?

 

On each post there are numerous comments which continue the discussion, such as WarpedJavaGuy:

DAO's will not die any time soon. Persistence and data access are not one and the same.
On the other hand, Antonio Goncalves replies:
Java EE has suffered of so much boiler plate code, design patterns that turned into anti-patterns, complexity and so on, that I have no problem in using EntitManager CRUD operations on my EJBs. For simple application, I skip the DAO pattern...

 

All in all the general consensus is that it all depends on the application and requirements. Adam sums it up,

I would say: it depends. It depends how complex your application really is.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Needs Criteria API

    by Jacob Hookom,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Tightly encapsulated DAOs can produce maintenance difficulties with managing optimal use cases. Fetching data optimally for all cases sometimes requires unconventional approaches to the DAO pattern where we've found the Criteria API from HB meets that. Your DAO maintains two methods, queryXXX and listXXX, allowing you to setup your biz assertions to produce a Criteria instance and then have the listXXX method just eval the Criteria returned by the queryXXX equivalent. By returning Criteria objects, you can still preserve the 'what', but allow web-actions to assert appropriate projections and fetch sizes based on optimal use cases much higher in the application stack.

    The problem is that the JPA does not include the Criteria API as of yet.

  • DAOs will be around for a little while yet

    by Billy Newport,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I don't think the DAO is dead yet. A JPA graph is still pretty much directly tied to the schema of the underlying data source. A DAO can potentially be decoupled from that schema and might represent a denormalized view or something like that. I can see such high level DAOs being implemented using code that makes use of JPA. A DAO is a higher level construct, not so tightly coupled with the underlying schema as JPA entities will be.

  • Re: DAOs will be around for a little while yet

    by Mohamed Ibrahim,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    You're right, the DAO won't die soon. But I think denormalization shouldn't be its job, you can create normalized views if you interfacing with a legacy/third party system or normalize your own schema to make it JPA/DAO friendly.

  • Re: DAOs will be around for a little while yet

    by Steven Devijver,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I believe claim for or against the DAO pattern don't stick. First of all, DAO is one of the most problematic abstractions because of it's leakage of concerns, especially but not only with OR mappers. You can't simply ignore this when considering the merits of the DAO pattern.

    On the other hand the DAO pattern is crucial for layering. However, no two applications are the same and especially due to it's leakage of concerns DAOs may be redundant.

    A far more interesting question (for me): is the DAO pattern crucial for good software quality? I still haven't figured that one out.

  • Re: DAOs will be around for a little while yet

    by Christian Schneider,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    In my opinion the reason why to have a DAO layer is to encapsulate database code and give the access function a name that plays a role in your domain model. So for example you could have a dao method like CustomerRepository.getCustomersWithCreditLimitLargerThan(Bigdecimal amount). This explains your intent much better than a JPA query. The art is then to make these expressive statements reusable and still focused on the domain task. Eric Evans talks about this a lot in Domain Driven Design though he does not really mention DAO. Perhaps the DAO code will simply be put in repository classes. Still it makes sense to encapsulate them.

  • Re: DAOs will be around for a little while yet

    by Handerson Gomes,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I think the way we know DAO's today will change. The idea of having a factory and interface is deprecated for EJB3 + JPA.

    Using the example above, without Christian permission, the method getCustomersWithCreditLimitLargerThan doesn't necessarily have to be in a DAO interface in order to be reused. A business object CustomerManager would expose this method along others which might not be related with a persistence layer but related with Customer Management (save, delete, getCustomersWhoBoughtItem(Item item), etc).

    I would not design a CustomerBean with methods like save, delete and query methods, most because of "cleanness" and aesthetic: Having all those JPA queries inside the POJO does look awful and defy the purpose of Plain Old Object.

    My understanding of what Adam's suggest is to have the persistence code inside a CustomerMgrBean instead of having it on a DAO layer or the CustomerBean itself.

    Regards,
    Handerson Gomes

  • Re: DAOs will be around for a little while yet

    by Christian Schneider,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    A business object CustomerManager would expose this method along others which might not be related with a persistence layer but related with Customer Management (save, delete, getCustomersWhoBoughtItem(Item item), etc).


    This is exactly the purpose of a Repository class: Handling Persistence and Multitudes of certain objects. As these operations don´t fit into the simple domain classes it makes sense to factor them out to special classes that handle these parts of the domain model. Still I think it makes sense to only keep the interface of the Repository (or Manager like you call it) in the domain layer. So you can separate the implementation and can swap it for a dummy implementation if you want to do unit tests without a container and db access.

  • Re: DAOs are not a layer

    by Daniel G.,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    First of all, DAOs can't be a layer since they have a dependency on business objects.
    And your facades aren't a layer because they have a dependency on DTOs?
    I personally think the DAO patterns is outdated and we should move to the Repository pattern as introduced in the book "Domain Driven Design" instead... *snip* I don't think the DAO pattern is outdated but we should switch to the cleaner Repository pattern instead.
    So... DAOs are outdated... but then you convince yourself that they aren't outdated?

    Color me confused!

  • Re: DAOs are not a layer

    by Steven Devijver,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    First of all, DAOs can't be a layer since they have a dependency on business objects.
    And your facades aren't a layer because they have a dependency on DTOs?


    DAOs are definitely a layer. Their arrangement package-wise is irrelevant.

  • Re: DAOs are not a layer

    by Steven Devijver,

    Your message is awaiting moderation. Thank you for participating in the discussion.


    Just to reiterate my point, DAOs are not a layer because one of the first law of the layer architectural pattern is that a lower layer is not allowed to have any dependencies on a upper layer. DAOs (or Repositories) have depencendies on your businness objects and therefore are a subpackage of the business layer.


    Who says "business objects" belong in the business layer, especially when they are mapped by a OR mapper? They might just as well belong to the data access layer.

  • JPA has not killed the DAO

    by WarpedJavaGuy ;,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Found an interesting post here about DDD and ORM backed Repositories. And a reaction to that post here.

    The post suggests that you can remove the need for DAO's by bridging aggregate level Repository abstractions to ORM Repository implementations. Strictly defined root aggregates and powerful ORM's can make it possible. It is a very indirect approach and it does require more work. Whether or not we embrace the approach and evolve it could depend on how well we adopt DDD. Either way, JPA has not killed the DAO. ORM's have been around for a while and we still write DAO's today. But that might change if (dare I say it) DDD kills the DAO!

  • Re: DAOs are not a layer

    by Steven Devijver,

    Your message is awaiting moderation. Thank you for participating in the discussion.


    Just to reiterate my point, DAOs are not a layer because one of the first law of the layer architectural pattern is that a lower layer is not allowed to have any dependencies on a upper layer. DAOs (or Repositories) have depencendies on your businness objects and therefore are a subpackage of the business layer.


    Who says "business objects" belong in the business layer, especially when they are mapped by a OR mapper? They might just as well belong to the data access layer.


    Well because the OR mapper have no hard dependency on your business objects in a so conceptually according to the layer pattern, those are two independants layers.


    Try loading your OR mapper without the "business objects" classes in your classpath. There is no static dependency but a dependency nonetheless.

    Classes that are mapped by an OR mapper - according to your layering theory - belong to the data access layer. The DAO/Repository/what-not can be a separate layer or part of the data access layer.

    And in the end it doesn't really matter. Databases and OR mappers are so leaky that you can't properly abstract them anyway.

  • Re: DAOs are not a layer

    by Daniel G.,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    LOL the joy of copy and paste and of a long day of work!
    And not enough coffee? ;-)

  • Separation of concerns?

    by Luis Fernando Planella Gonzalez,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I was once a DAO adept. But I've seen many times that it's simple impossible to perform real separation of concerns in practice using DAO.
    For example: on the project I work, we have an advertisement, which may be permanent or have a publication period. But we search by status, let's say active. Who should know what defines an active advertisement? The business layer, of course. But we must put that code on a HQL query, because we need to find advertisements by status.
    So, we DO NOT have separation of concerns here, because we also has a method isActive(ad) on the service layer...
    My opinion: bash the DAO layer.

  • Re: DAOs are not a layer

    by Dimitris Menounos,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    POJO "business objects" do not belong to either the service or the data acces layer. They form a layer in their own right, and actually a *vertical one* that cuts through the rest application layers.

    I searched for a visual representation and this is the closest I could find.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT