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

when I do

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

I got null point exception, and I debug got claimDataEntity .getEventDataEntityList() is null But actually this claimDataEntity have related event data in db

and I do it in UT like :

claimDataEntityRepository.findById(32L).get().getEventDataEntityList().parallelStream().forEach(eventDataEntity -> {
            log.info(eventDataEntity.getValidateEventDataId());
        });

It do log event data

So, why the function claimData get eventList as null???

--------------------v1----------------------------------------

I found it might not be the problem of stream, Actually I do a save before the iterator

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());

And I got both null of eventList

zhuochen shen
  • 1,673
  • 4
  • 16
  • 24

1 Answers1

1

Default fetchtype for @OneToMany is LAZY(Default fetch type for one-to-one, many-to-one and one-to-many in Hibernate). Hence it is not fetched. Make it EAGER

@OneToMany(mappedBy = "claimDataEntity", fetch = FetchType.EAGER)
private List<ValidateEventDataEntity> eventDataEntityList;
pvpkiran
  • 25,582
  • 8
  • 87
  • 134