Search code examples
web-applicationsjpadto

Manipulating JPA enities direclty in the web layer without DTOs


I work on an web application that uses JPA/EclipseLink together with Spring and Tapestry.

As of now, my web layer retrieves and displays the JPA entities directly without using any DTOs.

Is this a good idea? What are the pros and cons of my solution? What are the alternatives? Should I bother implementing DTOs?

Regards,


Solution

  • The JPA Entities are a plain POJOs and therefore (in a detached state) are a perfect example of a DTO. I wouldn't bother creating another layer of abstraction if it's not absolutely necessary.

    The situations, on top of my head, in which you might consider using explicit DTOs are:

    1. when you'd need a composite object to be used in the GUI layer which doesn't need (or can) use the fields directly from the JPA entities (i.e. combines data from different objects or uses only a part of the data and you don't want to let the client to access some other parts)
    2. when you're using the Domain Driven Design where the business logic is built around your entities. In this case, your entities are not simple Java Beans but fully fledged business logic components and therefore you might not want to expose them directly to the client.

    Always ask yourself a question - what it gives to you to introduce another layer of abstraction and if it's necessary in your particular case.

    HTH.