๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

๊ฐœ๋ฐœ/JPA5

[JPA] Specification์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ์กฐ๊ฑด ์ฒ˜๋ฆฌํ•˜๊ธฐ - ์™ธ๋ž˜ํ‚ค Specification Repository์— JpaSpecificationExecutor ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด Specification์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. @Repository public interface GrandChildRepository extends JpaRepository, JpaSpecificationExecutor { } โ€ป์ฐธ๊ณ  : ๋ณตํ•ฉํ‚ค์ด๊ธฐ ๋•Œ๋ฌธ์— JpaRepository๋ฅผ ์ƒ์†๋ฐ›์„ ๋•Œ ID๋ถ€๋ถ„์— Id class๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์ž [JPA] ๋ณตํ•ฉํ‚ค, ์™ธ๋ž˜ํ‚ค Entity ์„ค์ •ํ•˜๊ธฐ(@IdClass๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹๋ณ„๊ด€๊ณ„ ๋งคํ•‘) ์˜ฌํ•ด ์ดˆ ๋ณตํ•ฉํ‚ค, ์™ธ๋ž˜ํ‚ค ์‚ฌ์šฉ์ด ๋งŽ์€ ํ…Œ์ด๋ธ”์„ JPA Entity๋กœ ๊ตฌํ˜„ํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, JPA ์‚ฌ์šฉ์ด ์ฒ˜์Œ์ด์˜€์–ด์„œ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ๋ณต.. 2022. 1. 7.
[JPA] ๋ณตํ•ฉํ‚ค, ์™ธ๋ž˜ํ‚ค Entity ์„ค์ •ํ•˜๊ธฐ(@IdClass๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹๋ณ„๊ด€๊ณ„ ๋งคํ•‘) ์˜ฌํ•ด ์ดˆ ๋ณตํ•ฉํ‚ค, ์™ธ๋ž˜ํ‚ค ์‚ฌ์šฉ์ด ๋งŽ์€ ํ…Œ์ด๋ธ”์„ JPA Entity๋กœ ๊ตฌํ˜„ํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ, JPA ์‚ฌ์šฉ์ด ์ฒ˜์Œ์ด์˜€์–ด์„œ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„ ๋ณต์žกํ•˜๊ฒŒ ๋Š๊ปด์กŒ์—ˆ๋‹ค. ๋‚˜๊ฐ™์€ ์‚ฌ๋žŒ์„ ์œ„ํ•ด ์ •๋ฆฌํ•ด ๋ณธ๋‹ค! Entity์— ๋ณตํ•ฉํ‚ค๋ฅผ ํ†ตํ•œ ์‹๋ณ„๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ @IdClass, @EmbededId ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ ๋ณธ์ธ์€ @IdClass๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. @EmbededId๋Š” @IdClass ๋ฐฉ์‹ ๋ณด๋‹ค ์ข€๋” ๊ฐ์ฒด์ง€ํ–ฅ ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„๋ฅผ ๊นŠ๊ฒŒ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ณ , ๊นŠ์ด ๊ฐ์ถฐ์ ธ ์žˆ์–ด์„œ ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค๊ณ .. ์˜ˆ๋ฅผ ๋“ค์–ด grandChild Entity์—์„œ parent_id๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„  ์•„๋ž˜์™€ ๊ฐ™์ด ํƒ์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค. grandChild.getGId().getCId().getPI.. 2021. 12. 23.
[JPA] @OnDelete VS cascade = CascadeType.REMOVE ๊ด€๊ณ„ํ˜• DB์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” @OnDelete์™€ cascade = CascadeType.REMOVE์˜ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ณด์ž! @OnDelete DDMS ๋ ˆ๋ฒจ์—์„œ ์ž‘๋™ @OnDelete(action = OnDeleteAction.CASCADE) DDL ์ƒ์„ฑ์‹œ cascade ์ œ์•ฝ ์กฐ๊ฑด์ด ์ƒ์„ฑ ๋จ. ์—ฌ๊ธฐ์„œ DDL์ด๋ž€ Data Definition Language๋กœ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ์–ธ์–ด์ด๋ฉฐ, CREATE, ALTER, DROP, TRUNCATE๋ฅผ ๋งํ•œ๋‹ค. casecade=CascadeType.REMOVE JPA ๋ ˆ๋ฒจ์—์„œ ์ž‘๋™ JPA๊ฐ€ ๋ถ€๋ชจ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ญ์ œํ•  ๋•Œ ์—ฐ๊ด€๋œ ์ž์‹ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ DELETE ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ ํ•จ ๋ณธ์ธ์˜ ๊ฒฝ์šฐ ๋ถ€๋ชจ์™€ ์ž์‹์˜ 1:N ๊ด€๊ณ„์ผ ๋•Œ ์•„๋ž˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑ์‹œ์ผœ์•ผ ํ–ˆ๋‹ค. ์š”๊ตฌ.. 2021. 12. 13.
[JPA] ๋ฐ์ดํ„ฐ๋ฅผ insert ํ•˜๊ธฐ ์ „์— selectํ•˜๋Š” ์ด์œ  JPA๋ฅผ ์ด์šฉํ•˜์—ฌ DB์— ๋ฐ์ดํ„ฐ๋ฅผ insertํ•˜๊ฒŒ ๋˜๋ฉด id๋ฅผ ์กฐ๊ฑด์œผ๋กœ select๋ฅผ ๋จผ์ € ํ•˜๊ณ , insert๋ฅผ ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (JPA๋Š” ์กฐํšŒ ํ›„ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด update๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด insert๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.) ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋‹ค ๋ณด๋‹ˆ savaAll์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, xxxRepository.saveAll(entity); 1000๊ฑด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ๋“ฑ๋กํ•˜๋ ค๋ฉด select๋ฅผ 1000๋ฒˆ ์‹คํ–‰ํ•˜๋Š”..... JPA๊ฐ€ ํŽธ๋ฆฌํ•˜๊ธด ํ•œ๋ฐ.. ์ด๋Ÿฐ ๋ถ€๋ถ„์€.. ์ฐธ ๊ทธ๋ ‡๋‹ค ๊ทธ๋ž˜์„œ save() ๋ฉ”์†Œ๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๊ณ  ์žˆ๋Š”์ง€ ์‚ดํŽด๋ดค๋‹ค. @Transactional public S save(S entity) { if (entityInformation.isNew(entity)) { e.. 2021. 12. 2.
[JPA] @OneToMany - ์ž์‹ ํ…Œ์ด๋ธ”(๊ด€๊ณ„ Entity)์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ€์ ธ์˜ค๊ธฐ JPA๋ฅผ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด LAZY ํŒจ์น˜ํƒ€์ž…์œผ๋กœ relation์ด ๋‹ฌ๋ ค ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ n+1 ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” @EntityGrapth๋งŒ ๋‹ฌ์•„์ฃผ๋ฉด joinํ•˜์—ฌ ํ•œ๋ฒˆ์— selectํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜๋Š” ์˜ˆ์‹œ์ด๋‹ค. Product ํ…Œ์ด๋ธ”๊ณผ Sample ํ…Œ์ด๋ธ”์€ 1:N ๊ด€๊ณ„์ด๋‹ค. ๋”ฐ๋ผ์„œ sampleList์— @OneToMany ์–ด๋…ธํ…Œ์ด์…˜์„ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. (๋ฐ˜๋Œ€๋กœ Sample ์—”ํ‹ฐํ‹ฐ์— Product๋ฅผ @ManyToOne ์„ค์ •ํ•ด์ฃผ์–ด๋„ ๋œ๋‹ค) @Entity @ToString @Table(name = "PRODUCT") public class Product{ @Id @Column(name = "PRODUCT_ID", columnDefinition ="VARCHAR(36)") private .. 2021. 11. 26.
728x90
๋ฐ˜์‘ํ˜•