Object Mapping without losing existing data


Object Mapping without losing existing data



Usually Object Mappers are for scenarios to map a larger set of data to small set (example: Entity object has a lot of data but we want to return only a few of them).


Entity



Object mappers usually create a new Instance of destination object of small set and set the required fields from source object with larger set, but I have opposite scenario: I have a destination object which already contains some data and now I need to map a new source object with smaller data set into destination object.



Source Class


public class SrcObj {
private String name;
private int i;



Destination Class


public class DestObj {
private String name;
private int i;
private float f;
private boolean b;



DesObj already contains the value of int i and float f, SrcObj has String name and int, i need to save SrcObj data into DestObj without losing existing data.



Model Mapper always creating a new Object of DestObj which will map SrcObj into it but rest of the fields are null.


public class ObjectMapper {
public static void main(String args) {
SrcObj src = new SrcObj("src name", 1);
DestObj dest = new DestObj(null, 0, 1, true);
ModelMapper modelMapper = ObjectMapper.modelMapper();
dest = modelMapper.map(src, DestObj.class);
System.out.println(dest);
}

private static ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
PropertyMap<SrcObj, DestObj> orderMap = new PropertyMap<SrcObj, DestObj>() {
protected void configure() {
// map().setI(destination.getI());
map().setI(source.getI());
System.out.println("model mapper");
map().setName(source.getName());
}
};

modelMapper.addMappings(orderMap);
return modelMapper;
}
}



there is an option in Model mapper to use destination, commented above but it's not working.



how can i have destObj containing all values
destObj[name="src name", i=1,f=1.1,b=true]





You forgot to ask a question....
– Joakim Danielson
Jun 30 at 5:41





modelmapper.org/javadoc/org/modelmapper/…. But in my experience, such mappers should be avoided at all cost: they only save you a few lines of trivial code to copy properties from one object to another, but make things a lot harder when dealing with more complex mappings, imutable objects, etc. And most importantly, they fail silently when you rename a property in the source class and don't apply the same refactoring on the destination class (or vice-versa), leading to plenty of bugs that don' happen if you code the copy by yourself.
– JB Nizet
Jun 30 at 5:45






@JBNizet I already went through the documentation but didn't succeed. yes, I know the implications but right now I have simple objects so it doesn't matter.
– shrikant.sharma
Jun 30 at 5:48






Then post a complete minimal example of what you tried after using the method I linked to, and tell precisely what you expected to happen, and what happened instead.
– JB Nizet
Jun 30 at 5:50





HI @JBNizet I hope this is what you are saying modelMapper.map(src, dest); but its giving compile time error Type mismatch: cannot convert from void to DestObj because this overloaded method return type is void.
– shrikant.sharma
Jun 30 at 6:05



modelMapper.map(src, dest);




2 Answers
2



First, if you don't want to use getter/setter in your classes, you can just enable the field matching feature.


modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(AccessLevel.PRIVATE);



And you can simply modelMapper.map(src, dest) that will update the dest's properties from source.


modelMapper.map(src, dest)



@Chun Han Hsiao thanks for your response, it worked, just for the sake of solution.


SrcObj src = new SrcObj("src name", 1);
DestObj dest = new DestObj(null, 0, 1, true);
ModelMapper modelMapper = ObjectMapper.modelMapper();
modelMapper.map(src, dest);
System.out.println(dest);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV