DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Copy Bean Properties With a Single Line of Code

How to Copy Bean Properties With a Single Line of Code

Vineet Manohar user avatar by
Vineet Manohar
·
Sep. 01, 10 · Interview
Like (1)
Save
Tweet
Share
73.51K Views

Join the DZone community and get the full member experience.

Join For Free

This article shows how to copy multiple properties from one bean to another with a single line of code, even if the property names in the source and target beans are different.

Copying properties from one bean is quite common especially if you are working with a lot of POJOs, for example working with JAXB objects. Lets walk through the following example where we want to copy all properties from the User object -> SystemUser object

Example: source and target objects

// source object that we want to copy the properties from
public class User {
private String first;
private String last;
private String address1;
private String city;
private String state;
private String zip;
private String phone;

// getters and setters
// ...
}

// target object that we want to copy the properties to
public class SystemUser {
private String firstName;
private String lastName;
private String phone;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String zip;

// getters and setters
// ...
}

Example continued: Preparing the objects

// initializing the source object with example values
User user = new User();
user.setFirst("John");
user.setLast("Smith");
user.setAddress1("555 Lincoln St");
user.setCity("Washington");
user.setState("DC");
user.setZip("00000");
user.setPhone("555-555-5555");

// creating an empty target object
SystemUser systemUser = new SystemUser();

Approach 1: Traditional code for copying properties

 systemUser.setFirstName(user.getFirst());  
systemUser.setLastName(user.getLast());
systemUser.setPhone(user.getPhone());
systemUser.setAddressLine1(user.getAddress1());
systemUser.setAddressLine2(user.getAddress2());
systemUser.setCity(user.getCity());
systemUser.setState(user.getState());
systemUser.setZipcode(user.getZip());

Approach 2: Single line code for copying properties

copyProperties(user, systemUser, "first firstName", "last lastName", "phone",
"address1 addressLine1", "address2 addressLine2", "city", "state", "zip zipcode");

Parameters

  1. user – the source object
  2. systemUser – the target object
  3. first firstName – indicates that the “first” property of the source object should be copied to the “firstName” property of the target object
  4. last lastName – indicates that the “last” property of the source object should be copied to the “lastName” property of the target object
  5. phone – indicates that the “phone” property of the source object should be copied to the “phone” property of the target object
  6. …. and so on

The underlying code uses Apache Commons BeanUtils code to copy the property value from the source to the destination.

You can download the copyProperties code from Google Code. Simply copy the code to your project. The code requires apache BeanUtils. If you are using maven, you can get BeanUtils by adding the following to your pom.xml

 <!-- for maven only, add this to your pom to get BeanUtils, needed by the copyProperties code -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>

From http://www.vineetmanohar.com/2010/08/copy-map-bean-properties/

Property (programming) Bean (software) Spring Framework Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • Introduction to Spring Cloud Kubernetes
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • Building a RESTful API With AWS Lambda and Express

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: