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 is a simple, Plain Old Java Object which contains class properties and getter and setter methods for accessing those properties.
In this tutorial, I will use the BeanUtils class provided by a Spring Framework, but another way to map objects will be to use ModelMapper.
import org.springframework.beans.BeanUtils
the use of BeanUtils is very simple. To copy properties from a source Java object to a target Java object, a simple static method is used:
BeanUtils.copyProperties(sourceObject, targetObject);
Note: when using the BeanUtils.copyProperties() method, class field names in the sourceObject must match the class field names in the destination object. Have a look at the example DTO and Entity classes 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 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 above-mentioned DTO to an Entity object, we can use the following line of code:
UserEntity userEntity = new UserEntity(); BeanUtils.copyProperties(userDto, userEntity);
where:
- userDto is a source DTO object and
- userEntity is a target object.
And this is it! A very simple and very convenient way of copying properties from one bean to another.
BeanUtils – Ignore Certain Properties
When copying properties from an Entity object to a DTO object, sometimes, you might want to ignore certain properties. You can do it 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.
Watch the following Spring Boot video tutorials, to learn more.