Spring boot and hibernate : update all column's rows where entry is null

Viewed 169

I've created a query who select all users where a column is null :

@Query("SELECT user from User user where user.maths is null")
List <User> findByMathWhereNull();

Now I would like to calculate some points for each of them with a simple method :

public int calculate(User user){
        int maths = calculateByResults(user);
        maths += calculateByScore(user.getId());
        maths += calculateByBonus(user.getBonus());
        return maths;
    }

Now, I would like to set the maths variable for each user:

public void saveMaths() {
        List<User> users = userRepository.findByMathWhereNull();
        
        // TODO
        // for (maths : user) {do something}

how can I put each maths result in the correct column ?

EDIT - SOLUTION:

public void saveIndiceFiabilite() {
        List<User> users = userRepository.findByMathWhereNull();

         for (User user : users) {
             int math = calculateMaths(user);
             user.setMahs(maths);
             userRepository.updateUser(maths, user.getId());
         }

    }
1 Answers

U should setMath for each user like this:

    public void saveMaths() {
    List<User> users = organismeRepository.findByMathWhereNull();
    
    // TODO
       for(User user : users){
           int math = calculate(user);
           user.setMath(math);
       }
    
Related