问题描述
我在我的SpringBoot
应用程序中写了RestController
.我也在使用MongoDB
.这是我的实体:
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
和UserEntity
也是来自同一数据库的实体.我的RestController
的方法返回ResponseEntity
,因此默认情况下,内部使用Jackson
将Object
序列化为JSON
.我想确切地问一下惰性加载.如果我使用Jackson
中的@JsonIgnore
来忽略序列化中的该字段,那么ORM不会从数据库中获取惰性字段" 吗?
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?
谢谢您的帮助!
推荐答案
ORM仅在需要时才提取那些延迟加载的字段.这意味着,如果您指示Jackson在序列化过程中忽略它们(使用@JsonIgnore
注释),那么ORM将不会获取它们.
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.
这篇关于在SpringBoot Rest Controller中与Jackson @JsonIgnore合作进行MongoDB延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!