โ ๋ฌธ์ ์ํฉ
JPA์์ save() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ํฐํฐ๋ฅผ ์ ์ฅํ ๋,
@CreatedDate๊ฐ ์ค์ ๋ created_at ํ๋๊ฐ null๋ก ์ ์ฅ๋๋ ์ด์
๐ ์์ธ ๋ถ์
Spring Data JPA์ @CreatedDate๋ JPA Auditing ๊ธฐ๋ฅ์ ํตํด ์๋์ผ๋ก ๊ฐ์ ์ฃผ์ ๋ฐ๋๋ค.
์ด๋ Insert ๋ ๋ฌธ์ ๊ฐ ์์ผ๋ Update ์์๋ @CreatedDate ๊ฐ ์ค์ ๋ ํ๋์ Null ๊ฐ์ด ๋ค์ด๊ฐ๋ฒ๋ฆฐ๋ค.
๐ ๏ธ ํด๊ฒฐ ๋ฐฉ๋ฒ
๊ฒ์ํด๋ณธ ๊ฒฐ๊ณผ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ๋ค์ ๊ณ ๋ คํด๋ดค์ผ๋ ํด๊ฒฐ๋์ง ์์.
์ํฐํฐ์ @EntityListeners(AuditingEntityListener.class)๊ฐ ์์ํด๋น ํ๋์ @CreatedDate๋ง ์ ์ธ๋๊ณ , ํด๋์ค๊ฐ JPA๋ก ๊ด๋ฆฌ๋๋๋ก ์ ๋๋ก ์ค์ ๋์ง ์์Spring Boot ๋ฉ์ธ ํด๋์ค์ @EnableJpaAuditing ์ค์ ์ด ๋น ์ง@CreatedDate ํ๋๊ฐ null์ด ์๋ ๊ฐ์ผ๋ก ์ด๊ธฐํ๋์ด ์์ด ์ค๋ํ ์ด ๋ฌด์๋จ
๊ฒฐ๊ตญ update ์์ ๊ธฐ์กด ๊ฐ์ ๋ณต์ํด ์ค๋ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์๋ค. (ํด๊ฒฐ์๋ฃ)
@Override
@Transactional
public void update(ReadingNote readingNote) {
ReadingNote existing = readingNoteRespositoryPort.findByNoteId(readingNote.getNoteId())
.orElseThrow(() -> new RuntimeException("not found"));
readingNote.setCreatedAt(existing.getCreatedAt()); // ๊ธฐ์กด ๊ฐ ๋ณต์
readingNoteRespositoryPort.update(readingNote);
}