'Why the data resets after save in database?

I'm trying to save new phone number in database

First by another method get phone number and temporary save it and send a code to user then by following methods check if user entered correct code

The controller:

    @GetMapping("/check-code/{code}")
    public Boolean checkPhoneNumberCode(@PathVariable("code") int code){
        return phoneService.validateCode(code);
    }

The service:

    @Override
    public Boolean validateCode(int code) {
        Integer phoneNumberCode = cacheService.getPhoneNumberCode(phoneNumber.toString());
        if (!phoneNumberCode.equals(code)) {
            resendCode();
            return false;
        }
        updateUser();
        clear();
        return true;

    }

after the code was ok updateUser() method will call:

    @Override
    public void updateUser() {
        User user = userRepository.findByIdAndIsDeletedFalse(userId).get();
        user.setPhoneNumber(phoneNumber);
        userRepository.save(user);
    }

The UserRepository:

public interface UserRepository extends MongoRepository<User, String> {

And at the end of updateUser() method data must be updated and it updated but when updateUser() method finishes and validateCode() method finishes I've checked, before checkPhoneNumberCode() method finishes, data still updated (before "}" finishes and program exit from debug mode) but when program exit from debug mode (checkPhoneNumberCode() method completes) data will reset and back to what was before I'm using Intellij as IDE and Java for back-end and MongoDB for database I don't think it relates to front-end but I'm using angular for front-end

Github repository

Controller: src -> main -> ToDoWeb -> controller -> PhoneController; Service: src -> main -> ToDoWeb -> service -> impl -> PhoneServiceImpl; Repository: src -> main -> ToDoWeb -> repository -> UserRepository;



Solution 1:[1]

It would be difficult to know exactly what the problem is without taking a look at your repository. My guess would be that you're not implementing the repository well, and Spring is not able to create a Transaction by default. This question deals with something similar.

Check that your userRepository either extends from something like CrudRepository or JPARepository. Another option would be to annotate with @Transactional manually on the methods that modify the data in the database.


Also, check that your database creation policy is not set to CREATE, since this would re-create your data every-time the application is run. You'll probably find this in your application.properties file. (I doubt your problem is related to this, but there's no harm in making sure). You can find more information about this in this question.

hibernate.hbm2ddl.auto=update

I hope this helps, or at least it guides you in the right direction

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dave