Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting values using java reflection
    primarykey
    data
    text
    <p>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.</p> <p>I have:</p> <pre><code>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; } } </code></pre> <p>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.</p> <p>Please advice the efficient way of doing it.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CONot 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.
      singulars
    2. COHi 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)
      singulars
    3. COHonestly, 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.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload