• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

질문드립니다.

20.12.04 21:47 작성 조회수 122

1

안녕하세요. 개발 강의를 잘 듣고있는 개발자입니다. 먼저 덕분에 도움이 많이 되었습니다. 감사드립니다.

질문이 있는데

@Controller
@RequiredArgsConstructor
public class SettingsController {

private final AccountService accountService;

@GetMapping( "/settings/profile")
public String profileUpdateForm(@CurrentUser Account account, Model model){
model.addAttribute(account);
model.addAttribute(new Profile(account));
return "settings/profile";
}

@PostMapping( "/settings/profile")
public String updateProfile(@CurrentUser Account account, @Valid Profile profile, Errors errors, Model model,
RedirectAttributes attributes){
if(errors.hasErrors()){
model.addAttribute(account);
return "settings/profile";
}

accountService.updateProfile(account,profile);
attributes.addFlashAttribute("message","프로필을 수정했습니다."); // 한번 쓰고 말 데이터
return "redirect:/settings/profile";
}
}

해당 코드에서 @PostMapping 이후 @GetMapping으로 Redirect 된 후의 @CurrentUser Account account는 세션에 이미 있던 업데이트 되기 전의 계정이기 때문에 bio나 url이 없는 것으로 생각했는데요, 예상외로 잘 반영이 된 것 같아 어떻게 업데이트가 된 것인지 궁굼해서 질의드립니다.

감사합니다.^^

답변 2

·

답변을 작성해보세요.

2

네 모두 맞습니다.

2

[자체해결 및 another Question]

질문을 하고 다시 강의를 들었더니 11분 경에 하셨던 이야기가 들리기 시작했네요.

1. @CurrentUser 에 있는 Account 객체는 deteched  상태. 즉, 영속성 컨택스트 내부에 아이디가 존재하지만, 연결이 되지 않은 상태라 아무리 set해줘도 반영되지 않는 상태입니다.

2. 하지만 accountRepository가 save 구문을 실행해주면 deteched가 다시 영속성 컨택스트에 반영되면서 set 정보들이 merge 되게 된다. 따라서 account 객체도 업데이트 되고, DB에도 반영되는 효과를 가지게 된다.

3. account 객체는 세션에 있는 객체이지만, 이 save 구문을 통해 정보가 업데이트 되었다. 

로 3번을 추론해보았는데요. 해당 사항이 맞는지, 추가적으로 알아야 할 정보가 있는지 궁굼합니다. 우선 제가 먼저 드린 질문에 대한 해답은 이미 해주셨네요. 감사합니다.