DTO to Entity and Entity to DTO Conversion

Almost in every RESTful Web Service application, I have to do the DTO to Entity and then Entity to DTO conversion. DTO stands for Data Transfer Object and is a simple Plain Old Java Object which contains class properties and getters and settings methods for accessing those properties.

In this blog post, I am going to share with you how to copy properties from a DTO object to an Entity object and then back from an Entity object to a DTO object.

To copy properties from one bean to another I use the BeanUtils class provided by a Spring Framework:

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 one 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! Very simple and very convenient way of copying properties from one bean to another.

BeanUtils – Ignore Certain Properties

Sometimes when copying properties from an Entity object to a DTO object 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.

Interesting to learn more about Spring? Check out the below list of video courses and hopefully one of them will help to take your skills to a new level.