0
public class ValidateClaimDataEntity{
    ...
    @OneToMany(mappedBy = "claimDataEntity")
    private List<ValidateEventDataEntity> eventDataEntityList;
}

当我做

function(ValidateClaimDataEntity claimDataEntity){
   claimDataEntity
            .getEventDataEntityList().parallelStream()....
}

我得到空点异常,我调试得到claimDataEntity .getEventDataEntityList() is null 但实际上这个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 的空值

4

1 回答 1

1

默认 fetchtype 为@OneToManyLAZY(Hibernate 中一对一、多对一和一对多的默认获取类型)。因此它没有被提取。做了EAGER

@OneToMany(mappedBy = "claimDataEntity", fetch = FetchType.EAGER)
private List<ValidateEventDataEntity> eventDataEntityList;
于 2018-02-28T12:24:18.027 回答