Different ways to convert an Entity to a DTO in Java


In Java development, it is common to separate the concerns of the Data Layer (entity) and the presentation layer (DTOData Transfer Object). Entities represent the persistent data stored in a database, while DTO’s serve as a lightweight representation of that data for transferring across different layers or services.

In this blog, we will explore various techniques to transform/map an Entity class to a DTO with examples.

1. Manual Mapping

The simplest approach is to manually map the fields of the entity to the corresponding fields of the DTO. This can be done using setters and getters or constructors. Let’s consider an example where we have an entity class named User and a corresponding DTO class called UserDTO:

public class User {
    private String username;
    private String email;
    // ...

    // Getters and Setters
}

public class UserDTO {
    private String username;
    private String email;
    // ...

    // Getters and Setters
}

// Conversion
public UserDTO convertToDTO(User user) {
    UserDTO userDTO = new UserDTO();
    userDTO.setUsername(user.getUsername());
    userDTO.setEmail(user.getEmail());
    // ...

    return userDTO;
}

P.S. An entity class may have many DTO representation class depending on the project/business requirement.

2. ModelMapper Library

ModelMapper is a popular library that simplifies the mapping process by automatically mapping fields with the same names. To use ModelMapper, add the library to your project’s dependencies and create a mapper instance:

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.4.4</version>
</dependency>

Example:

ModelMapper modelMapper = new ModelMapper();
UserDTO userDTO = modelMapper.map(user, UserDTO.class);

3. MapStruct Library

MapStruct is another powerful library that generates mapping code at compile time, resulting in highly efficient mappings. It eliminates the need to write boilerplate mapping code manually.

To use MapStruct, you need to add the library as a dependency and create an interface specifying the mapping rules:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.2.Final</version>
</dependency>

Example:

// Conversion
@Mapper
public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDTO convertToDTO(User user);
}

// Usage
UserDTO userDTO = UserMapper.INSTANCE.convertToDTO(user);

Posts you may like:

  1. Java var Keyword: Enhancing Code Readability and Flexibility
  2. Java 8 Predicate vs Function: A Comparison with Examples
  3. Difference between ArrayList and Vector in Java

4. Lombok Library

Lombok is a popular library that helps reduce boilerplate code by providing annotations. It offers @Data and @Builder annotations, which are useful for generating constructors, getters, setters, and other utility methods. Lombok can simplify the process of creating DTOs with minimal code.

Add the Lombok dependency to the project build file:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>

Example:

UserDTO userDTO = UserDTO.builder()
        .username(user.getUsername())
        .email(user.getEmail())
        .build();

References

  1. Lombok Library
  2. MapStruct Library
  3. ModelMapper Library

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.