๐ŸŒป JAVA

๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

JPA - Fetch Join์ด ๊ณผ์—ฐ ๋งŒ๋Šฅ์ธ๊ฐ€? (N+1, Pagination)

๋“ค์–ด๊ฐ€๊ธฐ ์ „ ์ด์ „ ์‹œ๊ฐ„์— ์•Œ์•„๋ดค๋˜ N+1 ํ•ด๊ฒฐ๋ฒ•์— ์ด์–ด์„œ FetchJoin์„ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Fetch Join์ด๋ผ๊ณ  ๋‹ค ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Fetch Join์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์–ด๋– ํ•œ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ณ  ๊ทธ ํ•ด๊ฒฐ์ฑ…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. 1. FetchJoin, EntityGraph ์‚ฌ์šฉ ์‹œ Pagination์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. FetchJoin๊ณผ EntityGraph ๋‘˜ ๋‹ค ๋™์ผํ•œ ์ฆ์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Fetch Join๋งŒ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. @Query( value = "select t from Team t join fetch t.members", countQuery = "select count(t) from Team t" ) List f..

๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

[JPA] N+1 ๋ฌธ์ œ์™€ ํ•ด๊ฒฐ (feat. fetch join, EntityGraph)

๐Ÿ’ก ์•„๋ž˜ ์‹ค์Šต์— ์ง„ํ–‰ํ•œ ๋ชจ๋“  ์ฝ”๋“œ๋Š” Github์— ์žˆ์Šต๋‹ˆ๋‹ค. JPA N+1์ด๋ž€? ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋ฉด, N+1 ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ N+1์ด๋ž€ Team(1) ↔ Member(N) ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, ํ•˜๋‚˜์˜ ํŒ€์„ ์กฐํšŒํ–ˆ์ง€๋งŒ ํŒ€ ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ๋ฉค๋ฒ„๋“ค์ด ํ•จ๊ป˜ ์กฐํšŒ๋˜๋ฉด์„œ 1+N ๊ฐœ์˜ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ด๋Ÿฌํ•œ ์ƒํ™ฉ๋“ค์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด๊ณ  ๊ฐ ์ƒํ™ฉ์ด ๋˜ ์–ด๋–ค ์‚ฌ์ด๋“œ์ดํŽ™ํŠธ๊ฐ€ ์žˆ๋Š”์ง€๋„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Entity ๋ฐ Repository ์ฝ”๋“œ ์‹ค์Šต์— ์‚ฌ์šฉ๋  ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity public class Membe..

๐ŸŒป JAVA/์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž๋ฃŒ๊ตฌ์กฐ

ArrayList ๋‚ด๋ถ€๊ตฌํ˜„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ธฐ (feat. Array)

๊ฐœ์š” ArrayList๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ธ๋ฐ ArrayList๋Š” ์™œ Array์™€ ๋‹ค๋ฅด๊ฒŒ ์ดˆ๊ธฐ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋˜๊ณ , ๋ฌดํ•œ์ •์œผ๋กœ ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋Š˜ ์˜๋ฌธ์ด์˜€์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ArrayList๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๋ถ€ํ•ด๋ณผ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์›์†Œ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ค‘์ ์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ArrayList ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ๋˜์–ด์žˆ์„๊นŒ? public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8683452581122892189L; /** * Default..

๐ŸŒป JAVA/๋””์ž์ธ ํŒจํ„ด

Factory Pattern (feat. ์ถ”์ƒ ํŒฉํ† ๋ฆฌ, ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ)

1. ๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ ํŒจํ„ด ๋จผ์ € ๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ณ  ์ด๋ฅผ ์ ์ง„์ ์œผ๋กœ ํŒจํ„ด์„ ์ ์šฉํ•˜์—ฌ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•˜๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์•„๋ž˜์— ๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ type์— ๋”ฐ๋ผ์„œ ๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๊ณ  ์ค€๋น„, ์„ค์ •, ํฌ์žฅํ•ด์„œ ์ตœ์ข…์ ์œผ๋กœ ๋…ธํŠธ๋ถ์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. public NoteBook creatNoteBook(String type) { NoteBook noteBook; if(type.equals("Macbook")) { noteBook = new MackBook(); } else if (type.equals("LG Gram")) { noteBook = new LGGram(); } else if (type.equals("GalaxyBook")) { noteBook = new Ga..

๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

CascadeType.DELETE, orphanRemoval ์ฐจ์ด์ 

CascadeType.DELETE, orphanRemoval ์ฐจ์ด์  ๋ชฉํ‘œ CASCADE.REMOVE ์™€ orphanRemoval์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์šฉ ์—”ํ‹ฐํ‹ฐ public class Parent { @OneToMany(mappedBy = "parent") private List children = new ArrayList(); } public class Child { @ManyToOne @JoinColumn(name = "PARENT_ID") private Parent parent; } CASCADE.REMOVE @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE) private List children = new ArrayLi..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

Jackson ObjectMapper ๋น„์ง๋ ฌํ™” ๊ณผ์ •

ObjectMapper? Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์ธ ObjectMapper ํ†ตํ•ด JSON → Object(์—ญ์ง๋ ฌํ™”), Object → JSON(์ง๋ ฌํ™”)๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํŒŒ์‹ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. readValue() test.json { "name" : "john", "age" : 20 } json → Object ๋น„์ง๋ ฌํ™” ์ฝ”๋“œ People people = objectMapper.readValue(new File("static/test.json"), People.class); people์˜ ๊ฐ ๊ฐ’์„ ์ฐ์–ด๋ณด๋ฉด ์ž˜ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. System.out.println(people.getName() + ", " + people.getAge()); // john, 20 ๊ทธ๋Ÿฐ๋ฐ ๋ง์ž…๋‹ˆ๋‹ค.. Peopl..

iseunghan
'๐ŸŒป JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก