SoFunction
Updated on 2025-04-14

Summary of the method of copy conversion between DTO and Entity in Java

introduction

In Java development,DTO(Data Transfer Object)andEntity(Entity class) are two common data models. DTO is usually used for data transfer, while Entity is used to interact with a database. In actual development, it is often necessary to copy and convert data between DTO and Entity. This article will introduce several common tool classes and custom methods to implement this transformation, and provide corresponding code examples.

Manual copy

Manual copying is the most direct way to assign values ​​by writing code one by one.

Code Example

public class UserEntity {
    private Long id;
    private String name;
    private Integer age;
    // Omit the getter and setter methods}

public class UserDTO {
    private Long id;
    private String name;
    private Integer age;
    // Omit the getter and setter methods}

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) {
        UserDTO dto = new UserDTO();
        (());
        (());
        (());
        return dto;
    }

    public static UserEntity toEntity(UserDTO dto) {
        UserEntity entity = new UserEntity();
        (());
        (());
        (());
        return entity;
    }
}

advantage

  • Accurately controls the conversion logic of fields.
  • Not dependant on external libraries.

shortcoming

  • The code is lengthy and error-prone.
  • When there are many fields, the maintenance cost is higher.

Using BeanUtils

Apache Commons BeanUtils provides methods that simplify field copying.

Code Example

import ;

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) throws Exception {
        UserDTO dto = new UserDTO();
        (dto, entity);
        return dto;
    }

    public static UserEntity toEntity(UserDTO dto) throws Exception {
        UserEntity entity = new UserEntity();
        (entity, dto);
        return entity;
    }
}

advantage

Simplify code and reduce the workload of manual copying.

shortcoming

  • Need additional introductionApache Commons BeanUtilslibrary.
  • The performance is relatively low because it is throughreflectionAchieved.

Using Dozer

Dozer is an open sourceJava BeanMapping framework that supports complex data mapping.

Code Example

import ;

public class UserConverter {
    private static final DozerBeanMapper mapper = new DozerBeanMapper();

    public static UserDTO toDTO(UserEntity entity) {
        return (entity, );
    }

    public static UserEntity toEntity(UserDTO dto) {
        return (dto, );
    }
}

advantage

  • Supports complex data mapping, including nested objects and collections.
  • Flexible configuration, can be mapped through XML or annotations.

shortcoming

  • An additional Dozer library is required.
  • The configuration is relatively complex, especially for complex mapping scenarios.

Using MapStruct

MapStruct is an annotation-based code generation tool that can generate data mapping code at compile time.

Code Example

import ;
import ;

@Mapper
public interface UserMapper {
    UserMapper INSTANCE = ();

    UserDTO toDTO(UserEntity entity);

    UserEntity toEntity(UserDTO dto);
}

advantage

  • Generate code at compile time, high performance.
  • Supports complex data mapping, including nested objects and collections.
  • The code is concise and easy to maintain.

shortcoming

  • Need additional introductionMapStruct library.
  • Annotations are required, and the learning cost is high.

Using ModelMapper

ModelMapperIt's a simple and easy to useJava BeanMapping library that supports automatic mapping and custom mapping.

Code Example

import ;

public class UserConverter {
    private static final ModelMapper modelMapper = new ModelMapper();

    public static UserDTO toDTO(UserEntity entity) {
        return (entity, );
    }

    public static UserEntity toEntity(UserDTO dto) {
        return (dto, );
    }
}

advantage

  • Simple and easy to use, supports automatic mapping.
  • Supports custom mapping rules.

shortcoming

  • Need additional introductionModelMapperlibrary.
  • The performance is relatively low because it is achieved through reflection.

Custom tool classes

If the project has high performance requirements and the field mapping rules are relatively fixed, you can customize the tool class to implement field copying.

Code Example

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) {
        return new UserDTO((), (), ());
    }

    public static UserEntity toEntity(UserDTO dto) {
        return new UserEntity((), (), ());
    }
}

advantage

  • High performance because the constructor is called directly.
  • The code is concise and easy to maintain.

shortcoming

  • Complex data mapping is not supported.
  • If there are many fields, the number of code will increase.

Summarize

In actual development, which method to choose depends on the project requirements and the team's technology stack.

  • If the project has high performance requirements, it is recommended to useMapStruct or custom tool class;
  • If the project has high requirements for development efficiency, it is recommended to useModelMapper orBeanUtils

This is the article about the summary of the method of copy conversion of DTO and Entity in Java. For more related contents of copy conversion of Java DTO and Entity, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!