According to me, passing a persistable POJO, like for instance a bean managed by JPA, is not THE good practice.
Why?
I see three main reasons:
- Potential issue with lazy collections. http://java.dzone.com/articles/avoid-lazy-jpa-collections
- Entity should contain behaviour (in contrary of an Anemic domain model)
You may not want to let your UI call some unexpected behavior.
- In case of anemic domain model you may not want to expose your model structure to the UI, since every new change to model may break UI.
I prefer letting my service layer convert entities to corresponding DTO in both directions. DAO still returning entity (it's not its job to ensure the conversion).
Ok. Problem 1: getting DTO's from entities:
Since your entities can expose their data publicly you can access their properties and instantiate DTO object or simply serialise the entity directly
Problem 2 : Entities from DTO's:
A constructor method which takes a list of the properties to be set can be called using the properties of the DTO
Problem 3 : large entities when you only need summary info
Create a new summary object which you can retrieve from the repository. Note I suggest you make strongly typed repositories with methods like Repo.GetMyObjectById(string id) rather than expose a generic ORM.
Problem 4 : where to orchestrate all this.
My recommendation is to have a service class one level below your hosting service/app/website.
This has access to the repositories DTO's and entities and its methods map to the controllers/service calls of your application so that you do not need any code at the top level and can host the same service in multiple ways.
Giving it access to the repos is not an issue when you can only use them to retireve entities rather than doing any query they like.
Putting this assembly/orchestration logic inside the entity is usually bad as you will want the entity to be reused for other purposes.
This top level service should be very light. Just, get these objects, call this method, create and return the result/DTO/viewmodel
Because its so light its not a massive prob if you skip the layer and put the code in your controller. But it will save you time if you change the hosting layer and help with testing etc
Best Answer
Original MVC architecture
In the original MVC architecture (Xerox PARC in 1979), the controller handles the user interaction. It interacts with the model and with one or several views. The split of responsibility requires that the controller doesn't intervene in the rendering which is of responsibility of the view.
In this architecture, there is no need for services : it's perfectly valid for a controller to access domain objects directly.
MVC descendents
This is different from a MVP architecture (initially introduced by Talligent in 1996), where the presenter (aka controller) acts as middleman between the view and the model. The idea is that in a client-server architecture, the presenter could be split between the the server where the domain model is maintained, and the client that takes care of the view.
This is why, in MVP, the controller is supposed to only access the model through "commands" and "selections". So here a service layer should be used.
Service layer or not ?
This article makes the difference between MVC, MVP and MVVM clear.
Unfortunately frameworks often have their own understanding of MVC (e.g. Apple or Google Chrome team). It's not clear which MVC framework you are using : if needed edit your question to clarify.
Nevertheless, regardless of MVC, a domain could be supposed to be used by several application via an API (or implemented as a microservice). Then a service layer could be required because of other architectural considerations, and not because of MVC.