In this blog post, I will share how to copy properties from a DTO object to an Entity object and then back from an Entity object to a DTO object.
DTO stands for Data Transfer Object, and it is a simple, Plain Old Java Object that typically contains class properties and getter and setter methods for accessing those properties. It is commonly used to transfer data between different layers or modules of an application, or between different applications altogether. The purpose of using a DTO is to encapsulate the data and provide a standardized way of transferring data across different layers, which can help improve maintainability and scalability of the application.
In this tutorial, I will be using the BeanUtils class provided by the Spring Framework, but another way to map objects is by using ModelMapper.
import org.springframework.beans.BeanUtils
BeanUtils is a utility class provided by the Apache Commons BeanUtils library, which simplifies copying properties from one Java object to another by providing a set of static methods. One such method is the copyProperties
method, which copies the property values from a source object to a target object:
BeanUtils.copyProperties(sourceObject, targetObject);
Note that when using the BeanUtils.copyProperties() method, the field names in the sourceObject class must match the field names in the destination object class. Please refer to the example DTO and Entity classes provided below.
DTO Class
Let’s say we have the following DTO Java class:
package com.appsdeveloperblog.app.ws.shared.dto; import java.io.Serializable; public class UserDto implements Serializable { private static final long serialVersionUID = 4865903039190150223L; private long id; private String firstName; private String lastName; private String email; private String password; private String encryptedPassword; private String userId; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; }
Entity Class
And let’s say we have the following JPA entity class:
@Entity(name = "users") public class UserEntity implements Serializable { private static final long serialVersionUID = 4865903039190150223L; @Id @GeneratedValue private long id; @Column(length = 50, nullable = false) private String firstName; @Column(length = 50, nullable = false) private String lastName; @Column(length = 100, nullable = false) private String email; @Column(nullable = false) private String encryptedPassword; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getEncryptedPassword() { return encryptedPassword; } public void setEncryptedPassword(String encryptedPassword) { this.encryptedPassword = encryptedPassword; } }
Copy Properties from DTO to Entity
To copy properties from the mentioned DTO to an Entity object, you can use the following line of code:
UserEntity userEntity = new UserEntity(); BeanUtils.copyProperties(userDto, userEntity);
where:
- userDto is the source DTO object;
- userEntity is the target object.
And that’s it! A very simple and convenient way to copy properties between beans. You can also use ModelMapper to copy properties from one object to another.
BeanUtils – Ignore Certain Properties
When copying properties from an Entity object to a DTO object, there may be situations where you need to exclude specific properties. You can do this by using the following command:
BeanUtils.copyProperties(sourceObj, targetObj, "propertyToIgnoreA", "propertyToIgnoreB", "propertyToIgnoreC");
Just list the property names you want to ignore after the targetObj parameter.
To expand your knowledge, you can watch the following Spring Boot video tutorials.