๐ŸŒง๏ธ ORM/JPA

๐ŸŒง๏ธ ORM/JPA

JPA ๋ณตํ•ฉํ‚ค ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ (feat. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์•Œ์•„๋ณด์ž)

Intro ๋‹ค๋Œ€๋‹ค ํ…Œ์ด๋ธ”์„ ๋ณดํ†ต ์ผ๋Œ€๋‹ค ↔ ๋‹ค๋Œ€์ผ ํ…Œ์ด๋ธ”๋กœ ํ’€์–ด์„œ ๊ตฌ์„ฑํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„์— ์ด์–ด์ฃผ๋Š” ํ…Œ์ด๋ธ”์„ ์ค‘๊ฐ„ ํ…Œ์ด๋ธ”์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ์ด ํ…Œ์ด๋ธ”์˜ PK๊ฐ€ ์–‘์ชฝ ํ…Œ์ด๋ธ”์˜ PK๋ฅผ ๊ฐ€์ง€๊ณ  ๋ณตํ•ฉํ‚ค๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JPA์—์„œ ๋ณตํ•ฉํ‚ค๋กœ PK๋ฅผ ๊ตฌ์„ฑํ–ˆ์„ ๋•Œ, ์ €์žฅ ๋˜๋Š” PK๋ฅผ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์•ผํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœํ™˜๊ฒฝ SpringBoot 2.5.11 Java 11 H2 memory db ํ…Œ์ŠคํŠธ์šฉ ์—”ํ‹ฐํ‹ฐ ์†Œ๊ฐœ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity public class Member { @Id @GeneratedValue private Long id; private String name; @Ma..

๐ŸŒง๏ธ ORM/JPA

JPA Auditing์œผ๋กœ ์ƒ์„ฑ์ผ, ์ˆ˜์ •์ผ ์ž๋™ ์ƒ์„ฑ

Auditing ์ด๋ž€? ์—”ํ‹ฐํ‹ฐ๋Š” ๋‚˜์ค‘์— ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•ด์„œ ์ƒ์„ฑ์ผ๊ณผ ์ˆ˜์ •์ผ์„ ํ•„์ˆ˜์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒ์„ฑ์ผ๊ณผ ์ˆ˜์ •์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ์ผ public Book saveBook(BookDto bookDto) { // BookDto -> Book ๋ณ€ํ™˜ Book book = ...; book.setCreatedAt(LocalDateTime.now()); ... } ์ˆ˜์ •์ผ public Long updateBook(Long bookId, BookDto bookDto) { // ์ˆ˜์ • ๋กœ์ง book.setModifiedAt(LocalDateTime.now()); ... } ์ด๋Ÿฐ์‹์œผ๋กœ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ๋‹จ์ˆœ ๋ฐ˜๋ณต ์ฝ”๋“œ๊ฐ€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ Auditing์„ ์‚ฌ์šฉํ•˜..

iseunghan
'๐ŸŒง๏ธ ORM/JPA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก