mysql – I am not able to update my entity table (Spring Boot 2 hibernate 5)

mysql – I am not able to update my entity table (Spring Boot 2 hibernate 5)

I guess as you are doing save and saveOrUpdate concurrently that is causing the issue. Either, you should first close/flush the session via session.close() and then begin a new transaction for update.

Similar to like a sql query if its still processing although executed we use commit as to ensure the final changes have been made and are in existence and then we move to another transaction.

You made your program over-complex. With spring boot you dont need to implement dao class by yourself.
All you need to do is extending your dao from JpaRepository.

dao:

public interface UserAccountDao extends JpaRepository<UserAccount , Integer> {
}

service:

@service
public UserAccountService{
  @Autowired private UserAccountDao dao;

  public UserAcount saveAndUpdateUserData(UserAccount newAccount) {
    return dao.saveAndFlush(newAccount);
  }
}

controller:

@controller
public class TestController {
  @Autowired
  private UserAccountService userAccountService;

  @PostMapping(/test)
  public String addUser(UserAccount newAccount) {
      userAccountService.saveAndUpdateUserData(newAccount);
      return Success;
  }

}

mysql – I am not able to update my entity table (Spring Boot 2 hibernate 5)

Leave a Reply

Your email address will not be published. Required fields are marked *