jmapper-core icon indicating copy to clipboard operation
jmapper-core copied to clipboard

[Question] Map several source field to single destination object

Open petromir opened this issue 8 years ago • 5 comments

Hi,

I read about similar case here https://github.com/jmapper-framework/jmapper-core/wiki/Flatten-And-Encapsulate, but I couldn't match it to my case. So here it is:

public class Dto {
   private String data;
   private String text;
   private List<Long> ids;
}

public class Entity {
   private List<NestedEntity> nestedEntities;
}

public class NestedEntity {
   private String data;
   private String text;
   private Long id;
}

Dto dto = new Dto();
dto.setData("data");
dto.setText("text");
List<Long> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
dto.setIds(ids);

Entity entity = // JMapper getDestination method
entity.getNestedEntities().forEach(System.out::println)
// NestedEntity(data=data, text=text, id=1)
// NestedEntity(data=data, text=text, id=2)

What annotation should be placed in the Dtoin order to have a list of NestedEntity objects, which have same data located in data and text fields and different id taken from each item in ids

petromir avatar May 14 '17 22:05 petromir

Hi @petromir, There is currently no automation, default values are on the roadmap, meanwhile the only option is to use a static method only to encapsulate the mapping.

avurro avatar May 16 '17 08:05 avurro

@avurro Could you please provide an example?

petromir avatar May 17 '17 21:05 petromir

Hi @petromir, Actually looking better there is no way to encapsulate the mapping, but you gave me a good idea, that is to add a new placeholder that allows access to the root of the field, something like that: ${source.root} and ${destination.root} would be useful. What do you think about it ?

avurro avatar May 22 '17 07:05 avurro

Hi @avurro, Whatever you think will help us. Just provide few examples and probably a wiki article.

petromir avatar Jul 29 '17 16:07 petromir

I ran across a similar situation where I wanted to map two fields firstName and lastName onto one destination field fullName. I've been looking at global mappings and converters but nothing seems to fit the bill.

I'd like to do something like:

<!-- This is not working of course -->
<attribute name="fullName">
  <value>return ${source.root.firstName} + " " + ${source.root.lastName};</value>
</attribute>

Would that head into the same direction?

netmikey avatar Aug 18 '17 13:08 netmikey