1

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!

Piotr
  • 55
  • 8

1 Answers1

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.

Piotr Podraza
  • 1,941
  • 1
  • 15
  • 26