1
votes

I have an object which contains several list. is there a method to clone this object without id using mapstruct even for the nested object list in an automatic way to be able to persist it.

@Data
public class ParentDto {
  @Id
  private Long id;
  private String name;
  private List<Child1Dto> children1;
  private List<Child1Dto> children2;
}

@Data
public class Child1Dto {
  @Id
  private Long id;
  private String name;
}

@Data
public class Child2Dto {
  @Id
  private Long id;
  private String name;
}

Actual mapper

@Mapper(mappingControl = DeepClone.class)
public interface CloneParentMapper {
  @Mapping(target = "id", ignore = true)
  ParentDto cloneWithoutId(ParentDto parentDto );

  @Mapping(target = "id", ignore = true)
  Child1Dto cloneWithoutId(Child1Dto child1Dto );

  @Mapping(target = "id", ignore = true)
  Child2Dto cloneWithoutId(Child2Dto child2Dto );
}

is there a way to ignore all id without doing @Mapping(target = "id", ignore = true) on every list?

1
Can you show an example of the source object and target object which you want to map? - centrumek
Could be. Can you edit your question and share the code you've tried so far? What happens when you run it? What did you expect to happen instead? Any errors? If you don't have code yet, what have you tried and why didn't it work for you? See also How to Ask. - Robert

1 Answers

0
votes

I really feel this is the best and easiest way to ignore fields.

But still if u wanna ignore fields and not mark them as ignored specifically, then you can use constructors based mappings and have a separate constructor without id field. You will have to mark this constructor as @Default.

https://mapstruct.org/documentation/stable/reference/html/#mapping-with-constructors