DTO pattern: Best way to copy properties between two objects
You can use Apache Commmons Beanutils. The API is
org.apache.commons.beanutils.PropertyUtilsBean.copyProperties(Object dest, Object orig)
.
It copies property values from the "origin" bean to the "destination" bean for all cases where the property names are the same.
Now I am going to off topic. Using DTO is mostly considered an anti-pattern in EJB3. If your DTO and your domain objects are very alike, there is really no need to duplicate codes. DTO still has merits, especially for saving network bandwidth when remote access is involved. I do not have details about your application architecture, but if the layers you talked about are logical layers and does not cross network, I do not see the need for DTO.
You can have a look at dozer which is a
Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
Another better link...