Advantages of using Data Transfer Object in Java

Posted By Kaushlendra Kumar Pal | 29-Jul-2018

Data Transfer Object

A Data Transfer Object is an object that carries data and transferred between different processes. In client-server architecture communication between their different processes calls should be in optimized way otherwise it will be very expensive operation because cost of the calls are related to the round-trip time in between client-server so there is a way to reduce and optimize the number of calls between processes is to use DTO which is Data Transfer Object and is capable to collect the data from all the required objects which will transfer by the several specific calls but served by one call only.


To work with the remote interface each call is very expensive so you should have to reduce the number of calls in between data transfer. So the solution to this problem is to transfer data between processes in that object which can hold all the data in a single unit for which we can use a Data Transfer Object. In Java, DTO should be serializable to travel across the connection between client and server.


Since in communication between client-server, each call is very expensive so that calls should contain all the data which needed. So, if there is a case that multiple requests required to bring data for a single specific task so the data can be combined in a DTO so we can bring all the required data in one request only.


Since DTO is basically used to reduce the number of method calls means you can combine multiple POJO entities in the service layer.


For example: Suppose a GET request /rest/employee/101/department is to retrieve all the departments for employee id 101 with employee details so you need to combine all the data from employee entity and department entity with details.


Sample DTO:


public class EmployeeDto implements Serializable {

    private Long id;

    private String firstName;

    private String lastName;

    private String username;

    private Integer role;




DTO should implement serializable for a DTO if an object needs to transfer over the network in bytes from or data should be shared between different application or server.


Suppose, one application server serializes the data and other will access that data so receiver needs to deserialize that data.


DrawBacks of using Hashmap in place of DTO: -


Compile time checking not available: In hashmap, you can transfer data between client and server but you cannot get the compile-time checking for that data which can cause a big problem and also you cannot check that what you are casting it to is valid.


Refactoring problem: There are many IDEs available which provides the refactoring feature for very easy to use but this becomes much more difficult for dictionary pattern. -


Development problem: There is a big drawback to use the hashmap in place of DTO that new developers will take a much harder time to figure out what is in those hashmaps. They have to drill down many layers down to figure out what is going on in those hashmaps.


Return values ambiguity problem: Suppose, In a phase of a project, you are getting employee object and only you use the “getEmployeeName()” method. Then in further phase all of a sudden you need to have access to the department attribute. If you have an employee object, you know exactly if that value is accessible and how to get it by simply looking at the class. But with the hashmap, it's not easy because if you don't know the source of the class that generates the hashmap you will never know that what other data attributes are available.



Request for Proposal

Recaptcha is required.

Sending message..