[내일배움캠프] Spring 일정관리 앱 Develop Step2.

김재진·2026년 1월 8일

내일배움캠프

목록 보기
33/70

1. 일정관리 앱 Lv2. 유저 CRUD 생성과 일정 CRUD와 연결

  • 연관관계 구현
    • 일정은 이제 작성 유저명 필드 대신 유저 고유 식별자 필드를 가집니다.

2. github 주소

3. 유저와 일정의 연결

  • 일정 Entity안에 유저를 생성
public class Schedule extends BaseEntity{

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String scheduleTitle;
    private String scheduleContent;

	// 일정과 유저의 연결
    @ManyToOne(fetch = FetchType.LAZY, optional = false) // optional, nullable 은 동일해야 함
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    public Schedule(User user, String scheduleTitle, String scheduleContent) {
        this.user = user;
        this.scheduleTitle = scheduleTitle;
        this.scheduleContent = scheduleContent;
    }
  • controller
@PostMapping("/users/{userId}schedules") 
    public ResponseEntity<ScheduleCreateResponse> createSchedule(
            @PathVariable Long userId, // 유저PK 확인
            @Valid @RequestBody ScheduleCreateRequest request
    ){
        return ResponseEntity.status(HttpStatus.CREATED).body(scheduleService.save(userId, request));
    }
  • service
 @Transactional
    public ScheduleCreateResponse save(Long userId, ScheduleCreateRequest request) {
        User user = userRepository.findById(userId).orElseThrow( 
                () -> new IllegalStateException("없는 유저입니다.")
        );
profile
개발공부 처음해보는 사람

0개의 댓글