728x90
EventResponseDto.java
private User user;
public EventResponseDto(Event entity) {
this.user = entity.getUser();
}
EventServiceImpl
@Transactional
public Long save(final EventRequestDto params, User user) {
params.setUser(user);
Event entity = eventRepository.save(params.toEntity());
return entity.getId();
}
EventApiController
@PostMapping("/events")
public Long save(@RequestBody final EventRequestDto params, @AuthenticationPrincipal PrincipalDetail principalDetail) throws Exception {
return eventService.save(params, principalDetail.getUser());
}
1. @AuthenticationPrincipal 어노테이션 없이 로그인한 사용자의 정보를 불러오는 방법
출처: https://jaimemin.tistory.com/2078 [꾸준함:티스토리]
Event.java
domain
@Builder // 롬복에서 제공해주는 빌더라는 기능으로 생성자 대신에 이용하는 패턴. -> 생성자를 대신함.
public Event(String title, String content, int count, char deleteYN, User user){
this.title = title;
this.content = content;
this.count = count;
this.deleteYN = deleteYN;
this.user = user;
}
'개발 > JPA' 카테고리의 다른 글
jpa 배열로 넣기 (0) | 2022.08.10 |
---|---|
JPA 중요한 것 조인 칼럼을 쓸려면 (0) | 2022.08.10 |
(2) error-api - 전역 예외 처리(Global Exception Handling) & 로그백(logback-spring.xml) 적용하기 초기 설정 할 때 중요 작업 (0) | 2022.07.29 |
(1) error-api - 전역 예외 처리(Global Exception Handling) & 로그백(logback-spring.xml) 적용하기 초기 설정 할 때 중요 작업 (0) | 2022.07.29 |
Jpa Repository TLI 조회, 가져오기, Entity build & add.Params (0) | 2022.07.29 |