Home > OS >  @Scheduled not updating my JSON values to FALSE
@Scheduled not updating my JSON values to FALSE

Time:03-20

I'm trying to set all the values of a column of my H2 bank to false at 1:00 PM, but I'm having difficulties doing that, the message is being printed on the console, but the votes remain as TRUE.

My table looks like this, I would like every day at 1:00PM the values of the VOTED field to be FALSE

ID EMAIL PASSWORD NAME VOTED
1 [email protected] $2a$10$j1Eic8Okp.IczSVQbU.ru.s.dXoxd1fdzWtK2os9oE9y9ZO8wvMx6 Bruno TRUE
2 [email protected] $2a$10$F/KLm5ULaNRVEL/K6MMeveZXr770G5cI3S7HZnEw.b7TZDENkCWBC James TRUE
3 [email protected] $2a$10$uHZjSrroGBtRBB/V.norRuOcDVz42MXnm0/2yLPGpE3P5XtPRo7L6 Robert FALSE

This is the logic I'm using in my UserServices.java

Despite the project running, users remain as TRUE after the time determined in @Scheduled

@Scheduled(cron = "0 00 13* * *")
    public void resetVotes() throws InterruptedException {
            
    List<User> userArray = repository.findAll();
    for(User user: userArray ) {
        user.setVoted(false);
    }
}

And this is the message I get on the console when I arrive at the time I determined in @Scheduled

Hibernate: 
    select
        user0_.id as id1_1_,
        user0_.email as email2_1_,
        user0_.password as password3_1_,
        user0_.name as name4_1_,
        user0_.voted as voted5_1_ 
    from
        db_users user0_

Here are the classes I'm using for the project:

User.java

package com.dbserver.restaurantes.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "db_users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "Name")
    private String username;
    private String email;
    private String password;
    private Boolean voted = false;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getVoted() {
        return voted;
    }

    public void setVoted(Boolean voted) {
        this.voted = voted;
    }

}

UserDTO.java

package com.dbserver.restaurantes.dto;

import com.dbserver.restaurantes.entities.User;

public class UserDTO {
    private Long id;
    private String username;
    private String email;
    private String password;
    private Boolean voted = false;

    public UserDTO() {
    }

    public UserDTO(Long id, String username, String email, String password, Boolean voted) {
        this.id = id;
        this.username = username;
        this.email = email;
        this.password = password;
        this.voted = voted;
    }

    public UserDTO(User user) {
        id = user.getId();
        username = user.getUsername();
        email = user.getEmail();
        password = user.getPassword();
        voted = user.getVoted();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getVoted() {
        return voted;
    }

    public void setVoted(Boolean voted) {
        this.voted = voted;
    }

    
}

UserServices.java

package com.dbserver.restaurantes.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dbserver.restaurantes.entities.User;
import com.dbserver.restaurantes.exceptions.HttpClientErrorException;
import com.dbserver.restaurantes.repositories.UserRepository;

@Service
public class UserServices {

    @Autowired
    private UserRepository repository;

    PasswordEncoder passwordEncoder;

    public UserServices(UserRepository userRepository) {
        this.passwordEncoder = new BCryptPasswordEncoder();
    }

    @SuppressWarnings("rawtypes")
    @Transactional(readOnly = true)
    public List<User> findAllUsers(List list) {
        List<User> result = repository.findAll();
        return result;
    }

    @Transactional
    public User addUser(User newUser) {
        if (repository.findByEmail(newUser.getEmail()) != null) {
            throw new HttpClientErrorException("O e-mail informado já existe no sistema");
        }
        String encodedPassword = this.passwordEncoder.encode(newUser.getPassword());
        newUser.setPassword(encodedPassword);
        return repository.saveAndFlush(newUser);
    }
    
    @Scheduled(cron = "0 00 13* * *")
      public void resetVotes() throws InterruptedException {
        
        List<User> userArray = repository.findAll();
        for(User user: userArray ) {
            user.setVoted(false);
        }
      }

}

UserRepository.java

package com.dbserver.restaurantes.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import com.dbserver.restaurantes.entities.User;

public interface UserRepository extends JpaRepository<User, Long> {

    User findByEmail(String email);
}

UserController.java

package com.dbserver.restaurantes.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dbserver.restaurantes.entities.User;
import com.dbserver.restaurantes.services.UserServices;

@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private UserServices service;

    @PostMapping
    public User addUser(@RequestBody User newUser) {
        return service.addUser(newUser);
    }

}

CodePudding user response:

I think you need to re-update the user list after setVoted(false)

@Scheduled(cron = "0 00 13* * *")
public void resetVotes() throws InterruptedException {
            
    List<User> userArray = repository.findAll();
    for(User user: userArray ) {
        user.setVoted(false);
    }

    repository.save(userArray);
} 
  • Related