public class ValidateClaimDataEntity{
...
@OneToMany(mappedBy = "claimDataEntity")
private List<ValidateEventDataEntity> eventDataEntityList;
}
当我做的时候
function(ValidateClaimDataEntity claimDataEntity){
claimDataEntity
.getEventDataEntityList().parallelStream()....
}
我得到了零点异常,我调试了 claimDataEntity .getEventDataEntityList()为空 但实际上这个claimDataEntity在db
中有相关的事件数据我在UT中这样做:
claimDataEntityRepository.findById(32L).get().getEventDataEntityList().parallelStream().forEach(eventDataEntity -> {
log.info(eventDataEntity.getValidateEventDataId());
});
它记录事件数据
那么,为什么函数claimData将eventList作为null ???
-------------------- V1 ----------------------- -----------------
我发现它可能不是流的问题, 实际上我在迭代器之前做了一次保存
public ValidateClaimResponse bc(ValidateClaimRequest claimRequest) {
//claim
ValidateClaimDataEntity claimDataEntity = new ValidateClaimDataEntity(claimRequest);
claimDataEntityRepository.save(claimDataEntity);
claimRequest.getEventRequestList()
.forEach(eventRequest -> {
...
//event
ValidateEventDataEntity eventDataEntity = new ValidateEventDataEntity(eventRequest);
eventDataEntity.setValidateClaimDataId(claimDataEntity.getValidateClaimDataId());
eventDataEntityRepository.save(eventDataEntity);
});
System.out.println(claimDataEntity.getEventDataEntityList() == null ? "null" : claimDataEntity.getEventDataEntityList() );
ValidateClaimDataEntity claimDataEntity2 = claimDataEntityRepository.findById(claimDataEntity.getValidateClaimDataId()).get();
System.out.println(claimDataEntity2.getEventDataEntityList() == null ? "null2" : claimDataEntity2.getEventDataEntityList());
我得到了eventList
的null答案 0 :(得分:1)
@OneToMany
的默认抓取类型为LAZY(Default fetch type for one-to-one, many-to-one and one-to-many in Hibernate)。因此它没有取得。设为EAGER
@OneToMany(mappedBy = "claimDataEntity", fetch = FetchType.EAGER)
private List<ValidateEventDataEntity> eventDataEntityList;