Skip to content
Advertisement

Boolean value not being changed in database

Hey folks I’m having a problem with my code. For some reason when I try to change the value to false, it doesn’t reflect in my SQL Database. I debugged and it does get set via Java, but it doesn’t transfer over. I’m working on a notification service class and it sets the values on there to true no problem, but my PostMapping method, it doesn’t work. I will post a snippet of code below for both the add flight where I’m trying to do the logic and also my notification service.

@PostMapping("addflight")
String addflight(@ModelAttribute Flights flights, @SessionAttribute(required = false) String loggedInuser) {

    Accounts a = accountsRepository.findByEmail(loggedInuser).get();

    flightsRepository.saveAndFlush(flights);

    List<Flights> flightsList =  flightsRepository.findByAccountsOrderByDateofflightAsc(a);

    if(flightsList.size() != 0) {

        Flights lastFlight = flightsList.get(flightsList.size() - 1);
        LocalDate today = LocalDate.now();
        long days = 0;
        days = ChronoUnit.DAYS.between(lastFlight.getDateofflight(), today);
        if(days >= 46){
           a.setNotified(false);
        }

    }




    return "redirect:logbook";


}

#############################NOTIFICATION SNIPPET#########################################

 @Scheduled(fixedDelay = 4000000)
@Transactional
public void findFlights(){
    List<Accounts> list = accountsRepository.allpilots();
    List <Flights> accflight;
    long days = 0;
    long daysLeft = 0;

    for(Accounts a: list) {
        accflight = flightsRepository.findByAccountsOrderByDateofflightAsc(a);
        if (accflight.size() == 0) {
            System.out.println("No flights logged..");
            continue;
        }

        Flights lastFlight = accflight.get(accflight.size() - 1);

        days = ChronoUnit.DAYS.between(lastFlight.getDateofflight(), today);
        if (!a.getNotified()) {

            if (days > 60) {
                System.out.println("You are uncurrent! It has been " + days + " days since your last flight");

                try {
                    emailManager.sendMail(a.getEmail(), "You are uncurrent!", "Currency reminder");
                    a.setNotified(true);
                } catch (Exception e) {
                    System.out.println(e);
                    System.out.println("Email failed to send!");
                }
                continue;
            }

            

Advertisement

Answer

As far I can see: You are not updating your account after setting the notification to false. You need to persist your changes after setting it with something like accountRepository.save(a);

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