Java对象不会在方法外更改

时间:2019-08-06 06:03:41

标签: java hibernate object null

我正在从事Spring MVC项目。因此,这是我的控制器的登录方法:

@PostMapping("/login")
    public String login(
        Model model,
        @ModelAttribute("user") @Valid User user, BindingResult result) {
        if (result.hasErrors() )
            return "login-form";

        try {
            userService.login(user);
        } catch (UserNotFoundException | IncorrectPasswordException e) {
            model.addAttribute("message", "Incorrect email or password");
            model.addAttribute("tryAgainLocalHref", "/showLoginForm");
            return "authorization-failure-page";
        }

        //debug:
        System.out.println(user.getFirstName() + " " + user.getLastName());

        return "authorization-success-page";
    }

UserService的登录方法:

@Override
    @Transactional
    public void login(User user) throws UserNotFoundException, IncorrectPasswordException {
        User currentUser = userDAO.getUser(user.getEmail());
        if (currentUser == null)
            throw new UserNotFoundException();
        UserSecurity currentUserSecurity = getUserSecurityByUserId(currentUser.getId());
        String inputPasswordHash = 
            SecurityManager.hashPassword(user.getPassword(), currentUserSecurity.getSalt());

        if (!inputPasswordHash.equals(currentUserSecurity.getPasswordHash()))
            throw new IncorrectPasswordException();

        user = currentUser;
                //debug:
                System.out.println(user.getFirstName() + " " + user.getLastName());

    }

在这里您可以看到在UserService中,“ user”对象已替换为“ currentUser”。在我的业务逻辑中,“用户”只有两个非空字段:电子邮件和密码,而“ currentUser”有四个非空字段:电子邮件,密码,名字,姓氏。如您所见,在控制器的登录方法中,我打印用户的firstName和lastName。它写道:null null。但为什么???在UserService中将其替换为“ currentUser”!当我在UserService中打印用户的firstName和lastName字段时,它将从数据库中写入当前的firstName和lastName!那么,为什么“用户”对象更改对方法外部没有影响? “用户”是非基本类型...

0 个答案:

没有答案