search

Home  >  Q&A  >  body text

java - spring-data Jpa does not need to execute the save statement, but the Set field can automatically execute the save method? Solve

public Optional<User> requestPasswordReset(String mail) {
    return userRepository.findOneByEmail(mail)
        .filter(User::getActivated) 
        .map(user -> {// updata
            user.setResetKey(RandomUtil.generateResetKey());
            user.setResetDate(Instant.now()); 
            return user;
        });
    
}

//Enter the breakpoint, check the print results through the backend, and find that the select statement is executed first, and then the update statement is executed. I don’t quite understand why after set
//You can update without the save method

After following the code, when I get to this method, I execute the update statement. I ask the master to see it and teach me. I am very grateful! I will definitely pay attention to you.

仅有的幸福仅有的幸福2789 days ago1503

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-30 09:57:20

    Judging from your screenshot, I didn’t see where the update statement was executed. According to my experience, it will be updated only when the save method is called

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-30 09:57:20

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-30 09:57:20

    It should be that JPA discovered that your object is dirty and automatically executed it before Transaction commit.

    reply
    0
  • Cancelreply