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.
Asked
Active
Viewed 761 times
1
-
1There is `Cloneable` for this, but really, this is a job better left to a stored procedure in the database; it will do this much more efficiently than any Java code will ever achieve. – fge Feb 28 '14 at 17:54
-
So you want two instances of different classes (with no direct inheritance relationship) to have the same field (=instance variable) values? – Tobias Feb 28 '14 at 17:54
-
Cloneable is for the same class copy. If i understand that right, that are two different classes or is that my fault? – pL4Gu33 Feb 28 '14 at 17:56
-
possible duplicate of [Java: recommended solution for deep cloning/copying an instance](http://stackoverflow.com/questions/2156120/java-recommended-solution-for-deep-cloning-copying-an-instance) – Luiggi Mendoza Feb 28 '14 at 17:57
-
Crazy as it may sound, two different classes really with no common parentage. – TheMonkWhoSoldHisCode Feb 28 '14 at 17:58
-
I'd say pass a reference to the live one into the archive, and have the archive launch a thread that periodically copies the live one's contents... BUT BEWARE OF THREADSAFETY. – keshlam Feb 28 '14 at 18:00
3 Answers
3
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.

Frederic Close
- 9,389
- 6
- 56
- 67
1
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();
}
}

MoustafaAAtta
- 1,011
- 12
- 17
-
Thnx. The whole point was that I was copying the properties of the "current" object to "archive" object in my DAO code which wasn't looking pretty. This sound like a good idea. Basics, sometimes I forget. Thanks again. Cheers! – TheMonkWhoSoldHisCode Mar 01 '14 at 05:00
0
Look at the dozer mapper framework. Supports deep cloning using configuration.

Aragorn
- 5,021
- 5
- 26
- 37