1

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)
.
.
11
  • Whats the issue with recursive set of records? Commented Jun 13, 2019 at 7: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 Commented Jun 13, 2019 at 7: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 Commented Jun 13, 2019 at 7:21
  • And without the configuration of your ModelMapper we cant help you Commented Jun 13, 2019 at 7:23
  • 2
    Possible duplicate of Infinite Recursion with Jackson JSON and Hibernate JPA issue Commented Jun 13, 2019 at 7:25

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

This blog use MapStruct to mapping one to many relation;“Automating Code with MapStruct:medium.com/@swathisprasad/…
0

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

3 Comments

@AttilaT My answer contains more than just a link
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.
@AttilaT I indicated a specific place to use the link. That is why I quoted him exactly.
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.