
pk인 일정 id와, 생성일 수정일이 null로 반환이 되고 있음
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class CommonEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
확인 해보니 생성과 수정일을 가지는 CommonEntity 클래스가 추상화가 아니였음
상관 없을 거 같긴 하지만 abstract로 수정을 해봐도 증상은 동일
@Transactional
public CreateScheduleResponse createSchedule(CreateScheduleRequest request) {
Schedule newSchedule = new Schedule(
request.getScheduleName(),
request.getContents(),
request.getAuthorName(),
request.getSchedulePw()
);
return new CreateScheduleResponse(
newSchedule.getScheduleId(),
newSchedule.getScheduleName(),
newSchedule.getContents(),
newSchedule.getAuthorName(),
newSchedule.getCreatedAt(),
newSchedule.getModifiedAt()
);
}
ai한테 힌트를 요청했는데 답을 알려줘버렸음
문제는 post 요청을 받고 객체를 생성하고 나서
레포지토리에 save를 하지 않았었음
@Transactional
public CreateScheduleResponse createSchedule(CreateScheduleRequest request) {
Schedule schedule = new Schedule(
request.getScheduleName(),
request.getContents(),
request.getAuthorName(),
request.getSchedulePw()
);
Schedule newSchedule = scheduleRepository.save(schedule);
return new CreateScheduleResponse(
newSchedule.getScheduleId(),
newSchedule.getScheduleName(),
newSchedule.getContents(),
newSchedule.getAuthorName(),
newSchedule.getCreatedAt(),
newSchedule.getModifiedAt()
);
}
save 후 pk와 생성일, 수정일이 나오는 객체를 입력받고 return하게 수정

게시물을 동일한 사용자로 생성하면 문제가 발생한다.
@Getter
@Entity
@Table(name = "schedules")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Schedule extends CommonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long scheduleId;
@Column(length = 12, nullable = false)
private String scheduleName;
@Column(length = 512, nullable = false)
private String contents;
@Column(length = 50, nullable = false, unique = true)
private String authorName;
@Column(length = 50, nullable = false)
private String schedulePw;
public Schedule(String scheduleName, String contents, String authorName, String schedulePw) {
this.scheduleName = scheduleName;
this.contents = contents;
this.authorName = authorName;
this.schedulePw = schedulePw;
}
}
이유는 내가 authorName 은 유일키라고 생각을 했기 때문
근데 생각해보면 동일한 사용자가 일정을 2개 이상 등록하는 경우도 당연히 있음
authorName의 unique를 제외하기로 했음


경로 파라미터로 전달하니 예외가 발생
@GetMapping("/schedules/{scheduleId}")
public ResponseEntity<GetScheduleResponse> getOneSchedule(
@PathParam("scheduleId") Long scheduleId
) {
return ResponseEntity.status(HttpStatus.OK).body(scheduleService.getOneSchedule(scheduleId));
}
PathParam 로 되어 있던게 문제 -> PathVariable 로 변경

PathVariable 쪽애 노란색으로 경고 문구가 나왔다.
Remove redundant path variable name declaration
이건 PathVariable의 값인 {scheduleId}와 변수 이름 Long scheduleId 이 같기 때문에
생략을 해도 된다는 것이다.
PathVariable과 변수 이름이 다를 경우에만 ("") 을 명시해주면 된다.

일정 수정 시 패스워드 검증을 받는데
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "com.example.schedule.dto.PatchScheduleRequest.getSchedulePw()" is null
request 객체의 변수가 null로 보임

디버깅 모두를 보면 requet 변수 안에 있는 값이 모두 null
@PatchMapping("/schedules/{scheduleId}")
public ResponseEntity<PatchScheduleResponse> patchSchedule(
@PathVariable Long scheduleId,
PatchScheduleRequest request
) {
return ResponseEntity.status(HttpStatus.OK).body(scheduleService.patchSchedule(scheduleId, request));
}
매개변수 쪽에 보면 @RequestBody 가 빠져있었음
추가 후 정상

분명 6번 을 수정하기 했고 리턴도 정상인데

6번 내용이 전혀 수정이 되지 않음
로그를 보니
Hibernate:
select
s1_0.schedule_id,
s1_0.author_name,
s1_0.contents,
s1_0.created_at,
s1_0.modified_at,
s1_0.schedule_name,
s1_0.schedule_pw
from
schedules s1_0
where
s1_0.schedule_id=?
update 관련 쿼리가 보이지 않음
public PatchScheduleResponse patchSchedule(Long scheduleId, PatchScheduleRequest request) {
Schedule getSchedule = scheduleRepository.findById(scheduleId).orElseThrow(
() -> new IllegalStateException("없는 일정 ID 입니다."));
// 패스워드가 다를 경우
if (!request.getSchedulePw().equals(getSchedule.getSchedulePw())) {
throw new IllegalStateException("패스워드가 틀렸습니다.");
}
getSchedule.updateSchedule(request.getScheduleName(), request.getAuthorName());
return new PatchScheduleResponse(
getSchedule.getScheduleId(),
getSchedule.getScheduleName(),
getSchedule.getContents(),
getSchedule.getAuthorName(),
getSchedule.getModifiedAt()
);
}
업데이트 메서드 위에 @Transactional 가 빠져 있었음
추가를 하고 나서는 업데이트 쿼리는 정상 실행
이번엔 업데이트 쿼리에서 Data truncation: Data too long for column 'schedule_name' at row 1 발생
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long scheduleId;
@Column(length = 12, nullable = false)
private String scheduleName;
@Column(length = 512, nullable = false)
private String contents;
@Column(length = 50, nullable = false)
private String authorName;
@Column(length = 50, nullable = false)
private String schedulePw;
public Schedule(String scheduleName, String contents, String authorName, String schedulePw) {
this.scheduleName = scheduleName;
this.contents = contents;
this.authorName = authorName;
this.schedulePw = schedulePw;
}
public void updateSchedule(String scheduleName, String authorName) {
this.scheduleName = scheduleName;
this.authorName = authorName;
}
scheduleName을 12자로 제한했는데 그걸 넘어섰기 때문
충분하다고 생각하여 12로 했었는데 50으로 수정