简体   繁体   中英

Spring MVC / Spring Data data fetch recursion

I have an entity with one-to-many relationship (for ex. Person o->m Book ). If I want to fetch a person with books in controller, it causes recursion. @JsonIgnore by FasterXML helps, but what if I want bidirectional fetch without recursion. For example fetch Person with Books and fetch Book with Persons ?

Use @JsonBackReference

class Person{
    @OneToMany(mappedBy="person",fetch = FetchType.EAGER)
    private List<Book> books; 
    ...
}

class Book { 
    @ManyToOne
    @JoinColumn(columnDefinition="integer", name = "person", nullable=false)
    @JsonBackReference
    private Person person;
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM