This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
we have developed an application using spring and hibernate, here we are actually passing model objects from dao layer to service and from service to controller and from there to jsp . we are suggested by someone that models shouldn't be sent from one layer to other instead use DTO's to transfer the data from one layer to another layer. we are confused that should we go for dto's or we go with models only. please tell us which is the correct way to transfer the data and why.
First, I will say this and mean it very much DTO is an ANTI-PATTERN. Meaning it was created to fix a major poor design in Entity Beans in Java EE.
Now with that in mind. There are some domain objects or use cases where you don't want DTOs and you want your POJO domain model. That to me is at least 80% of the time you will send those objects from one layer to the other.
If you create DTOs for all those layers then you will have a lot of "Adapter" code to convert your domain objects to DTOs and back the other way. This is extremely unmaintainable and error prone code. Personally I would run far far away if I ever saw this code in a project I was to work on. I have seen it way too many times and it is sad.
Now there are some use cases where you need "reports" or report data" This tends to be a report, where you need data from lots of tables combined to make a page, and creating a large graph of related domain objects can be well complex. Then a Report Domain object (almost like a DTO) would be better than data domain objects.
But there are ORM tools that make those simpler, like iBatis and even JPA/Hibernate has things like "Select new ReportDomain(field1, field2, field3) from Domain d" etc