0

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;
}

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.

6
  • 2
    Not an answer, but related to your problem at hand - why are you using DTOs and Entities at the same time? You could use detached entities to act as DTOs, and therefore avoid the problem of setting values to an Entity, as it is already available in the Entity. Commented Jul 24, 2011 at 3:05
  • Hi Vineet, Thanks for the good idea. Reasons y i want to use both DTOs and Entity. 1 - To have a loose coupling b/w service layer (which manages biz logic and entities) and servlet controllers. 2 - My understanding is that, for the very first time when we persist an entity, we cannot use detached entity. 3 - When fetching an entity, it may have all the values loaded with (say for example: 20 instance variables. Know.. that we can use lazy loading to fix some of it). If i use DTO, instead of sending the fully loaded Entity, i can use a lightweight version of DTO (ex: with just 5 values) Commented Jul 24, 2011 at 17:16
  • Honestly, point 2 is moot, as a detached entity is meant to exist only after an entity is committed. So, if you are persisting an entity, it is a new/transient instance, going by the JPA entity lifecycle. As far as point 1 and 3 are concerned, DTOs help only if they are not replicas of the entities (which is not the case in your posted code). If you are sending 5/20 fields of an entity in a DTO having 20 fields, it makes no sense as you are simply duplicating fields. Worse, adding a field to a datamodel will require changes in two classes instead of one. Commented Jul 24, 2011 at 17:24
  • Yes, Vineet, your comments make sense and agree with it. Another related question: When the controllers receive the form fields from the user, what is the best way to validate it? can we use constraint annotations or hibernate validators or what else? which is the best way to handling the validation?. Thanks for your help. Commented Jul 24, 2011 at 21:39
  • Well, there's JSR 303 bean validation. Hibernate Validator implements JSR 303. Commented Jul 25, 2011 at 5:49

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.