Java 8 java stream将Spring数据jpa OneToMany集合获取为null
我得到了空点异常,调试得到了claimDataEntity .getEventDataEntityList()为空 但实际上这个claimDataEntity在db中有相关的事件数据 我在UT中这样做:Java 8 java stream将Spring数据jpa OneToMany集合获取为null,java-8,spring-data,java-stream,jquery-lazyload,Java 8,Spring Data,Java Stream,Jquery Lazyload,我得到了空点异常,调试得到了claimDataEntity .getEventDataEntityList()为空 但实际上这个claimDataEntity在db中有相关的事件数据 我在UT中这样做: function(ValidateClaimDataEntity claimDataEntity){ claimDataEntity .getEventDataEntityList().parallelStream().... } 它不记录事件数据 那么,为什么函
function(ValidateClaimDataEntity claimDataEntity){
claimDataEntity
.getEventDataEntityList().parallelStream()....
}
它不记录事件数据
那么,为什么函数claimData get eventList为null
--------------v1-------------------------------------------
我发现可能不是流的问题,
实际上,我在迭代器之前进行了一次保存
claimDataEntityRepository.findById(32L).get().getEventDataEntityList().parallelStream().forEach(eventDataEntity -> {
log.info(eventDataEntity.getValidateEventDataId());
});
我得到了eventList的两个null对于
@OneToMany
的默认fetchtype是LAZY()。因此它不会被提取。让它成为渴望的
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());
@OneToMany(mappedBy=“claimDataEntity”,fetch=FetchType.EAGER)
私有列表eventDataEntityList;
使用fetchType作为默认类型
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());
@OneToMany(mappedBy = "claimDataEntity", fetch = FetchType.EAGER)
private List<ValidateEventDataEntity> eventDataEntityList;