KSK
KSK

Reputation: 636

How to set class members values to another class

I have two classes one is vo class for DB layer and another one is Bean class for view layer. But two classes are have the same members, in order to save the values in db I set all the field values from Bean class to VO class. Then I pass the VO class to hibernate or procedure.

Example vo.setId(bean.getId()); etc..

Is it any other way to set the values from Bean to VO and VO to Bean? Like( vo=bean || bean=vo )

Upvotes: 0

Views: 106

Answers (2)

Marcinek
Marcinek

Reputation: 2117

You can you apache BeanUtils:

http://commons.apache.org/proper/commons-beanutils/

Here you have the following method:

org.apache.commons.beanutils.BeanUtilsBean.copyProperties();

As you stated your vos and beans have the same attributes. This method will copy the properties.

Upvotes: 1

k0ner
k0ner

Reputation: 1116

If these are different objects there isn't. You can create constructor which does what you need:

public SomeClass1(SomeClass2 other) {
    this.id = other.getId();
    ....
}

Upvotes: 0

Related Questions