Skip to content
Advertisement

How to check not null date in spring jpa

I have 2 IF. If No 1, is work for me. Because i wont if data is present and get house keeping == null. for action to update condition,status,and housekeeping.

If(getData.isPresent() && bulkUpdate.getHouseKeeping() == null)

IF no 2, no work for me. I wont if getData is present and get housekeeping is availabe / not null. For action only to update condition

else if(getData.isPresent() && bulkUpdate.getHouseKeeping() != null)

Only check ‘null’, but not for ‘not null’. I wont check null and not null is work for me

NB : getData.isPresent = Integer , getHouseKeeping = Date

Full code :

Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date today = sdf.parse(sdf.format(now)); 

    RoomBoy updatedData = request.getPayload();

    Optional<RoomBoy> getData = roomBoyRepository.findByIdAndHouseKeeping(updatedData.getId(), updatedData.getHouseKeeping());
    if(getData.isPresent() && updatedData.getHouseKeeping() == null ) {
        RoomBoy oldData = getData.get();

        oldData.setCondition(updatedData.getCondition());
        oldData.setStatus("Done");
        oldData.setHouseKeeping(today);

        roomBoyRepository.saveAndFlush(oldData);
        status.setStatusCode("000");
        status.setStatusDesc("Success");
    }else if(getData.isPresent() && updatedData.getHouseKeeping() != null){
        RoomBoy oldData = getData.get();
        oldData.setCondition(updatedData.getCondition());

        roomBoyRepository.saveAndFlush(oldData);
        status.setStatusCode("000");
        status.setStatusDesc("Success");
    }else{
        status.setStatusCode("001");
        status.setStatusDesc("Data Not Found");
    }

Advertisement

Answer

I faced somewhat similar issue with comparing null. Try doing the following :

if(getData.isPresent()){
     if(updatedData.getHouseKeeping() == null ) {
        RoomBoy oldData = getData.get();

        oldData.setCondition(updatedData.getCondition());
        oldData.setStatus("Done");
        oldData.setHouseKeeping(today);

        roomBoyRepository.saveAndFlush(oldData);
        status.setStatusCode("000");
        status.setStatusDesc("Success");
    }else{
        RoomBoy oldData = getData.get();
        oldData.setCondition(updatedData.getCondition());

        roomBoyRepository.saveAndFlush(oldData);
        status.setStatusCode("000");
        status.setStatusDesc("Success");
    }
}
else{
        status.setStatusCode("001");
        status.setStatusDesc("Data Not Found");
}

Here we have changed the arrangement of if-elseif to nested if-else. I hope it helps.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement