Hibernate OneToOne属性未加载时弹簧座数据投影
我有三门课:Hibernate OneToOne属性未加载时弹簧座数据投影,hibernate,spring-rest,spring-projections,Hibernate,Spring Rest,Spring Projections,我有三门课: @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 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 */
}
@实体
公务舱旅行{
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
私人名单;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
私人名单详情;
/*剪断*/
}
@实体
公共类TripDetail{
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
私人定制组件定制组件;
/*剪断*/
}
@实体
公共类自定义组件{
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
私有列表现有组件;
/*剪断*/
}
这个预测:
@Projection(name = "withUsages", types = { Trip.class })
public interface TripWithUsagesProjection {
List<LegWithUsagesProjection> getLegs();
List<TripDetail> getDetails();
}
@Projection(name=“withUsages”,types={Trip.class})
公共接口TripWithUsagesProjection{
列出getLegs();
List getDetails();
}
现在,当我使用投影对trips API进行GET时,返回的JSOn中TripDetail中的customComponents对象为null
如果我将TripDetail中的customComponents属性更改为立即加载(fetch=FetchType.EAGER),则生成的JSON是正确的(因为它包含customComponents的数据内联)
我想知道为什么会这样
TripDetail还有一系列为简洁起见未显示的其他属性(一些@OneToMany、一些BigDecimal、字符串和其他属性)。这是唯一的@OneToOne。为什么@OneToOne在这里的行为会有所不同?看看这段代码
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private CustomComponents customComponents;
您正在告诉Hibernate以惰性方式加载CustomComponents。i、 e它将仅在需要时加载其详细信息
因此,如果您正在调试并查找customComponents,它将为null
但是,当您尝试从CustomComponents读取一些数据时,它应该加载CustomComponents详细信息
因为Hibernate会在引擎盖下发出另一个查询
select * from CustomComponents where id = ?
懒惰就是这样工作的
更多细节