简体   繁体   English

为什么保存到数据库后数据会重置?

[英]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: 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:代码确定后 updateUser() 方法将调用:

    @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-endupdateUser()方法结束时,数据必须更新并且更新但是当updateUser()方法完成并且validateCode()方法完成时我检查过,在checkPhoneNumberCode()方法完成之前,数据仍然更新(在“}”完成之前和程序从调试模式退出)但是当程序从调试模式退出时( checkPhoneNumberCode()方法完成)数据将重置并返回到我使用 Intellij 之前的状态,后端为 IDE 和 Java,数据库为 MongoDB 我不不认为它与前端有关,但我在前端使用 angular

Github repository Github 资料库

Controller: src -> main -> ToDoWeb -> controller -> PhoneController ; Controller: src -> main -> ToDoWeb -> controller -> PhoneController Service: src -> main -> ToDoWeb -> service -> impl -> PhoneServiceImpl ;服务:src -> main -> ToDoWeb -> 服务 -> impl -> PhoneServiceImpl Repository: src -> main -> ToDoWeb -> repository -> UserRepository ;存储库: src -> main -> ToDoWeb -> 存储库 -> UserRepository

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.我的猜测是您没有很好地实现存储库,并且 Spring 默认情况下无法创建Transaction This question deals with something similar. 这个问题涉及类似的事情。

Check that your userRepository either extends from something like CrudRepository or JPARepository .检查您的userRepository是否从CrudRepositoryJPARepository等扩展而来。 Another option would be to annotate with @Transactional manually on the methods that modify the data in the database.另一种选择是在修改数据库中数据的方法上手动使用@Transactional进行注释。


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.此外,请检查您的数据库创建策略是否未设置为CREATE ,因为这会在每次运行应用程序时重新创建您的数据。 You'll probably find this in your application.properties file.您可能会在application.properties文件中找到它。 (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我希望这会有所帮助,或者至少它会引导您朝着正确的方向前进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM