开发者

Setting values using java reflection

Thanks in advance for your time and help. Looked into other posts but only pieces of info available, so if someone gives the complete picture, greatly appreciate it.

I have:

public enum AddressType {
HOME,WORK,BILLING,SHIPPING,OTHER
}

public class AddressDto implements java.io.Serializable {

private String street;
private String city;
private String stateCode;
private int zipcode;
private String country;
private AddressType addressType;

public AddressDto() {
}

public String getStreet() {
    return street;
开发者_如何学Python}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getStateCode() {
    return stateCode;
}

public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}

public int getZipcode() {
    return zipcode;
}

public void setZipcode(int zipcode) {
    this.zipcode = zipcode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public AddressType getAddressType() {
    return addressType;
}

public void setAddressType(AddressType addressType) {
    this.addressType = addressType;
}
}

@Entity
@Table(name = "ADDRESS")
public class Address implements java.io.Serializable {

private String street;
private String city;
private String stateCode;
private int zipcode;
private String country;
private AddressType addressType;

public Address() {
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getStateCode() {
    return stateCode;
}

public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}

public int getZipcode() {
    return zipcode;
}

public void setZipcode(int zipcode) {
    this.zipcode = zipcode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public AddressType getAddressType() {
    return addressType;
}

public void setAddressType(AddressType addressType) {
    this.addressType = addressType;
}
}

Using reflection, i am trying to to get values from DTO and set values to an Entity. Why reflection? Thinking that i can re-use this reflection code for all other similar cases where there is a DTO and entity involved.

Please advice the efficient way of doing it.

Thank you.


You can, probably use Apache BeanUtils to do that. Class BeanUtils has appropriate methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜