我没有获取@ManyToOne字段的数据。
@Entity
public class UserDetail {
@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name = "user_id")
@JsonIgnore
private List<UserOrder> orders;
}
@Entity
public class UserOrder {
@ManyToOne
@JoinColumn(name = "user_id")
@JsonBackReference
private UserDetail userDetail;
@ManyToOne
@JoinColumn(name = "runner_id")
@JsonBackReference(value="runner")
private UserDetail runner;
}
List<UserOrder> findAllByStatusNotInOrderByOrderDateDesc(List<OrderStatus> orderStatuses);
当我获取订单列表时,我期望结果是userdetail和Runner,但在JSON输出中却什么也没得到。
答案 0 :(得分:0)
您需要在结果中包括延迟加载关系。
@EntityGraph(attributePaths = "runner")
List<UserOrder> findAllByStatusNotInOrderByOrderDateDesc(List<OrderStatus> orderStatuses);
或者将其设置为紧急加载
答案 1 :(得分:0)
尝试以这种方式设置这两个实体之间的关系:
@Entity
public class UserDetail {
@OneToMany(mappedBy="userDetail", cascade = { CascadeType.ALL })
@JsonIgnore
private List<UserOrder> orders;
@OneToMany(mappedBy="runner", cascade = { CascadeType.ALL })
@JsonIgnore
private List<UserOrder> runnerOrders;
}
@Entity
public class UserOrder {
@ManyToOne
@JoinColumn(name = "user_id")
@JsonBackReference
private UserDetail userDetail;
@ManyToOne
@JoinColumn(name = "runner_id")
@JsonBackReference(value="runner")
private UserDetail runner;
}