TheMonkWhoSoldHisCode
TheMonkWhoSoldHisCode

Reputation: 2332

Cloning different objects in Java

I have two database tables which are mirror images of each other. The reason for this is one table stores "CURRENT" values and the other tables saves "ARCHIVED" values. I use hibernate as the ORM tool. The tables have 20 columns each . The business requirement is that the values in the "current" table gets house-keeped at specific intervals to "archive" table. It is cumbersome to copy the values from "current" object to "archive" object. Is there a way in JAVA to clone objects of different types (current object to archive object)? The elements of the objects are identical.

Upvotes: 1

Views: 900

Answers (3)

Aragorn
Aragorn

Reputation: 5289

Look at the dozer mapper framework. Supports deep cloning using configuration.

Upvotes: 0

MoustafaAAtta
MoustafaAAtta

Reputation: 1081

I go with what fge suggested in the comment, however, you can just create a constructor and pass to it the "current" object.

class ArchiveEntry{
   private String dummy;

   public ArchiveEntry(CurrentEntry entry) {
      this.dummy = entry.getDummy(); 
   }
}

Upvotes: 1

Frederic Close
Frederic Close

Reputation: 9639

Have a look at Apache Commons BeanUtils

It has usefull method to copy properties between two distinct without hierarchy relationship. This should work as long as the propperties of both beans have the same names.

BeanUtils.copyProperties(Object dest, Object orig);

Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

Upvotes: 3

Related Questions