Java返回null而不是对象的列表

时间:2018-07-10 13:38:37

标签: java jquery mysql spring jpa

我有一个我不明白的问题。 我有以下代码:

@Override
public List<MissionProfile> populateFilterMissionProfile(List<Integer> fleet, List<Integer> application, List<Integer> vehicle, List<String> dataSet, List<String> shift, List<String> drchannel, List<String> swVersion, String userName){ 
    //If vehicle is null (so not used into filter) than add the allowed vehicles (for the user) to the query to filter the acquisitions by the allowed applications
    if (vehicle==null) {
        vehicle =fleetHasUserRepository.getAllCarForUser(userName).stream().map(x->x.getIdCar()).collect(Collectors.toList());
    }
    //Retrieve all the acquisitions for the specific parameters
    List<Acquisition> acquisitions = acquisitionRepository.advancedSearch(fleet, application, vehicle, null, dataSet, shift, null, null ,drchannel, swVersion);
    List<MissionProfile> missionProfiles = acquisitions.parallelStream().map(x->x.getMissionProfile()).distinct().collect(Collectors.toList());
    return missionProfiles;
}

此返回的null列表(由Web服务调用并返回{"status":true,"success":true,"result":[null,null],"error":null}

我在许多其他方法中使用相同的代码,唯一的区别是另一种使用List<String>List<Integer>。 我想错误是在advancedSearch方法内部,但是如果我在返回之前放一个System.out.println(missionProfiles),或者如果我用eclipse debug看了这个变量,它就会起作用,否则返回null(即使我写了missionProfiles.size()无效,但使用missionProfiles.get(0).getidMissionProfile()则有效)
这是数据库代码

@Query("SELECT e "
            + "FROM Acquisition e "
            + " WHERE (COALESCE( :fleet ) is null OR e.car.fleet.fleetName.idFleetName in (:fleet) )"
            + " AND  (COALESCE( :application ) is null OR e.car.fleet.idFleet in (:application) )"
            + " AND (COALESCE( :vehicle ) is null OR e.car.idCar in (:vehicle) )"
            + " AND (COALESCE( :mp ) is null OR e.missionProfile.idMissionProfile in (:mp) )"
            + " AND (COALESCE( :dataset ) is null OR e.dataset in (:dataset) )"
            + " AND (COALESCE( :shift ) is null OR e.shift.idShift in (:shift) )"
            + " AND (:dateFrom is null OR  e.date >= :dateFrom) "
            + " AND (:dateTo is null OR  e.date <= :dateTo) "
            + " AND (COALESCE( :drchannel ) is null OR e.drChannelsConf in (:drchannel) )" 
            + " AND (COALESCE( :swVersion ) is null OR e.swVersion.idSwVersion in (:swVersion) )" )
    List<Acquisition> advancedSearch( @Param(value = "fleet") List<Integer> fleet,
            @Param(value = "application") List<Integer> application,
            @Param(value = "vehicle") List<Integer> vehicle,
            @Param(value = "mp") List<Integer> mp,
            @Param(value = "dataset") List<String> dataSet, 
            @Param(value = "shift") List<String> shift, 
            @Param(value = "dateFrom") Date dateFrom, 
            @Param(value = "dateTo") Date dateTo,
            @Param(value = "drchannel") List<String> drchannel,
            @Param(value = "swVersion") List<String> swVersionn );

我尝试不使用流并且使用Thread.sleep(10000),但是问题仍然存在。看到错误了吗?
这是采集的调试 enter image description here 这是任务 enter image description here

1 个答案:

答案 0 :(得分:0)

关于Lazy实体上的acquisition初始化是一个问题。 我添加了这一行

missionProfiles.forEach(x->Hibernate.initialize(x.getMissionProfile()));

return

之前