Cooperation MongoDB lazy loading with Jackson @JsonIgnore in SpringBoot Rest Controller

删除回忆录丶 提交于 2021-01-29 03:08:29

问题


I have written a RestController in my SpringBoot app. I am using a MongoDB as well. This is my entity:

public class LocationEntity {

    @Id
    private String id;

    private String name;

    @DBRef(lazy = true)
    @JsonIgnore
    private UserEntity owner;

    private String description;

    @DBRef(lazy = true)
    private List<RoleEntity> roles;

    private Date date;

    public LocationEntity(String name, UserEntity owner, String description, List<RoleEntity> roles, Date date) {
         this.name = name;
         this.owner = owner;
         this.description = description;
         this.roles = roles;
         this.date = date;
    }
}

RoleEntity and UserEntity are entities from the same database as well. My RestController's methods return ResponseEntity, so by default Jackson is used inside to serialize Object to JSON. I would like to ask about lazy loading precisely. If I use @JsonIgnore from Jackson to ignore that field in serialization, will ORM not get "lazy fields" from database?

Thanks for help in advance!


回答1:


ORM will only fetch those lazy loaded fields when needed. That means if you instruct Jackson to ignore them (using @JsonIgnore annotation) during serialization, ORM will not fetch them.



来源:https://stackoverflow.com/questions/58064639/cooperation-mongodb-lazy-loading-with-jackson-jsonignore-in-springboot-rest-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!