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

entity2

[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.
728x90
๋ฐ˜์‘ํ˜•