【发布时间】:2017-12-28 16:37:30
【问题描述】:
我有 2 个实体
产品:
@Id @GeneratedValue
private Long id;
private String name;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Product parentProduct;
@OneToMany(fetch = FetchType.LAZY)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<Product> childProduct;
@OneToMany(mappedBy="product", fetch = FetchType.LAZY)
@JsonManagedReference @JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<Image> images;
图片:
@Id
@GeneratedValue
private Long id;
private String type;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JsonBackReference
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Product product;
当我从 RestController 调用时正在加载惰性关系,但是当我从主方法(Spring Boot)调用时它们为空。
Json 序列化时我做些什么来保持懒惰。
json 返回:
[ {
"id" : 1,
"name" : "Passatempo Pacote",
"description" : "Cookies Package",
"images" : [ {
"id" : 2,
"type" : "png"
}, {
"id" : 1,
"type" : "jpeg"
} ]
}, {
"id" : 2,
"name" : "Passatempo",
"description" : "Cookies",
"parentProduct" : {
"id" : 1,
"name" : "Passatempo Pacote",
"description" : "Cookies Package",
"images" : [ {
"id" : 2,
"type" : "png"
}, {
"id" : 1,
"type" : "jpeg"
} ]
}
} ]
图像必须为空,因为属性上的延迟配置
【问题讨论】:
标签: json rest spring-boot spring-data-jpa