Framework/Orm&Mapper

[JPA] repository.save()의 반환값이 안 들어올 때

simDev1234 2022. 10. 19. 15:00

|  상황

- repository.save()를 하면 Entity타입의 객체가 반환되어야 하는데 아래와 같이 null이 반환됐다.

/*Q14*/
// 트러블 슈팅 : 어? 뭐지? 데이터가 저장은 됐는데, Response값이 안온다.
// -- 원인 ) Entity 인스턴스 비교(equals)를 위해 equals, hashcode 필요
@PostMapping("/api/notice4")
public Notice addNotice4(@RequestBody NoticeRegister noticeRegister){
    return noticeRepository.save(
        Notice.builder()
                .title(noticeRegister.getTitle())
                .contents(noticeRegister.getContents())
                .build()
    );
}

>> Response

{}

 

|  해결방법

- Entity 객체에 @EqualsAndHashCode가 추가되면 된다.

- 나는 그냥 @Data를 넣어주었다.

 

[참고]

https://stackoverflow.com/questions/68429396/jparepository-save-methods-returns-null