0

I am using springBoot, I want to map OneToMany relationship from parent to child. Directly using entity with eager fetching I get recursive records, therefore, trying using ModelMapper for DTO mapping to Entity but I am not able to figure out how to do it. Please assume getters and setters.

Parent.java

@Entity
public class Parent {

    @Id
    private int parentId;

    private String a;

    @OneToMany(mappedBy = "parent")
    private Set<Child> child;

Child.java

@Entity
public class Child {

    @Id
    private int childId;

    private String c;

    @ManyToOne
    @JoinColumn(name = "b")
    private Parent parent;

I have working repository and servicelayer with findAll method.

ParentDto.java

public class ParentDto {

    private String a;

    private Set<Child> child;

ParentController.java

@RestController
public class ParentController {

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private ParentService parentService;


    @RequestMapping(method = RequestMethod.GET, value="/parents" )
    public List<ParentDto> getParents() {
        List<Parent> parents =  parentService.getAll();
        return parents.stream()
                .map(x-> modelMapper.map(x, ParentDto.class))
                .collect(Collectors.toList());
    }
}

Error: While trying to fetch http://localhost:8080/parents

.
.
ModelMapper mapping errors: 1) Converter org.modelmapper.internal.converter.CollectionConverter@51381583 failed to convert java.util.Set to java.util.Set. 1 error
org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.CollectionConverter@51381583 failed to convert java.util.Set to java.util.Set.

1 error
    at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:80)
.
.
garg10may
  • 5,794
  • 11
  • 50
  • 91
  • Whats the issue with recursive set of records? – XtremeBaumer Jun 13 '19 at 07:18
  • 1) Directly passing domain/entity to frontend is not recommended. 2) By recursive I mean infinite loop of `parent->child->parent->child.....` and JVM crashes. That's obviously a problem. I might get around that problem by using `eager` at parent and `lazy` at child though haven't tried, for the same I switched to `DTO` – garg10may Jun 13 '19 at 07:20
  • And that is why `EAGER` is **not** the recommended fetch type... You can also always write your own custom mapper for domain model to DTO – XtremeBaumer Jun 13 '19 at 07:21
  • And without the configuration of your `ModelMapper` we cant help you – XtremeBaumer Jun 13 '19 at 07:23
  • 2
    Possible duplicate of [Infinite Recursion with Jackson JSON and Hibernate JPA issue](https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – Alan Hay Jun 13 '19 at 07:25
  • @XtremeBaumer Yes :( and I am not able to configure it. I tried to use `LOOSE` strategy but that didn't help and I don't see any examples of configuring nested objects. – garg10may Jun 13 '19 at 07:25
  • You don't need a DTO. https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue – Alan Hay Jun 13 '19 at 07:25
  • @AlanHay thanks for this but really I am looking to use `DTO` for future uses also. I might get around for this example but have other cases to handle. – garg10may Jun 13 '19 at 07:27
  • You manually need to break the recursion by setting parent inside child of main parent object as null, before mapping entity to dto. – Jignesh M. Khatri Jun 13 '19 at 07:28
  • https://github.com/modelmapper/modelmapper/issues/89 You really need to simply google the exception – XtremeBaumer Jun 13 '19 at 07:31
  • There are better strategies for controlling the JSON representation of your entities than duplicating your entity model as DTO classes and writing tedious mapping code. Jackson mix-in classes and Spring Data projections for example. – Alan Hay Jun 13 '19 at 07:49

3 Answers3

0

This has nothing to do with “N + 1” and “Infinite Recursion” issue ,May be add Conver method to mapper; Mapping Children with ModelMapper https://amydegregorio.com/2018/10/03/mapping-children-with-modelmapper/

helleye
  • 46
  • 4
  • This blog use MapStruct to mapping one to many relation;“Automating Code with MapStruct:https://medium.com/@swathisprasad/automating-code-with-mapstruct-44b8c324a23e” – helleye Feb 29 '20 at 02:08
0

I propose "Preferred nested properties".
http://modelmapper.org/user-manual/configuration

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
vi0
  • 197
  • 1
  • 7
  • @AttilaT My answer contains more than just a link – vi0 Nov 20 '22 at 01:30
  • 1
    Prefixing a link with a sentence that contains only five words, three out of which are direct quote from the page linked is essentially a link-only answer. – Attila T Nov 21 '22 at 01:15
  • @AttilaT I indicated a specific place to use the link. That is why I quoted him exactly. – vi0 Nov 21 '22 at 13:37
-1

You need to use @JsonIgnore annotation on the parent mapping defined in your child entity.

Refer this documentation for more info - https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonIgnore.html

4b0
  • 21,981
  • 30
  • 95
  • 142
TeJas
  • 68
  • 3
  • 7