【发布时间】:2018-04-30 21:47:37
【问题描述】:
我有这三个类:
@Entity
public class Trip {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Leg> legs;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<TripDetail> details;
/* snip */
}
@Entity
public class TripDetail {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private CustomComponents customComponents;
/* snip */
}
@Entity
public class CustomComponents {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<CustomComponent> existingComponents;
/* snip */
}
还有这个投影:
@Projection(name = "withUsages", types = { Trip.class })
public interface TripWithUsagesProjection {
List<LegWithUsagesProjection> getLegs();
List<TripDetail> getDetails();
}
现在,当我使用投影对旅行 API 执行 GET 时,返回的 JSOn 中 TripDetail 中的 customComponents 对象为空。
如果我将 TripDetail 中的 customComponents 属性更改为快速加载 (fetch=FetchType.EAGER),那么生成的 JSON 是正确的(因为它包含 customComponents 的内联数据)。
我想知道为什么会这样?
为简洁起见,TripDetail 有许多其他属性未显示(一些 @OneToMany、一些 BigDecimal、字符串和其他属性)。这是唯一的@OneToOne。为什么 @OneToOne 在这里表现不同?
【问题讨论】:
标签: hibernate spring-rest spring-projections