๐Ÿ๏ธ Spring

GroupBy ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด GroupBy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. having์„ ํ†ตํ•ด ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์…‹์—… ์ฝ”๋“œ @BeforeEach fun before() { val teamA = Team(name = "teamA") val teamB = Team(name = "teamB") em.persist(teamA) em.persist(teamB) val members = listOf( Member(username = "member1", age = 10, team = teamA), Member(username = "member2", age = 20, team = teamA), Member(username = "member3", age = 30, team = teamB), Memb..
์ง‘๊ณ„ (count, sum, avg, max, min) ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ๊ทธ๋ฃน ๋ณ„๋กœ ๊ทธ๋ฃนํ•‘๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. COUNT() : ์ด ๊ฐœ์ˆ˜ SUM() : ํ•ฉ AVG() : ํ‰๊ท  MAX() : ์ตœ๋Œ€๊ฐ’ MIN() : ์ตœ์†Œ๊ฐ’ ์‚ฌ์šฉ ์˜ˆ์‹œ ์…‹์—… ์ฝ”๋“œ @BeforeEach fun before() { val teamA = Team(name = "teamA") val teamB = Team(name = "teamB") em.persist(teamA) em.persist(teamB) val members = listOf( Member(username = "member1", age = 10, team = teamA), Member(username = "member2", age = 20, team = teamA), Member(..
์ •๋ ฌ ์ •๋ ฌ์€ orderBy()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. desc(), asc() : ๋‚ด๋ฆผ์ฐจ์ˆœ, ์˜ค๋ฆ„์ฐจ์ˆœ nullsLast() : null ์ธ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. nullsFirst() : null ์ธ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ฒ˜์Œ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. setup ์ฝ”๋“œ @BeforeEach fun before() { val teamA = Team(name = "teamA") val teamB = Team(name = "teamB") em.persist(teamA) em.persist(teamB) val members = listOf( Member(username = "member1", age = 10, team = teamA), Member(username = "member2", age = 20, team ..
๊ฒฐ๊ณผ ์กฐํšŒ ๋ฉ”์†Œ๋“œ fetch() : ๋ฆฌ์ŠคํŠธ ์กฐํšŒ, ๋ฐ์ดํ„ฐ ์—†๋Š” ๊ฒฝ์šฐ ๋นˆ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ fetchOne() : ๋‹จ ๊ฑด ์กฐํšŒ ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด null ๊ฒฐ๊ณผ๊ฐ€ ๋‘˜ ์ด์ƒ์ด๋ฉด NonUniqueResultException fetchFirst() : limit(1).fetchOne()๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ QueryDSL 5.0 ๋ฒ„์ „์—์„œ Deprecate๋œ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. fetchResult() : ํŽ˜์ด์ง• ์ •๋ณด ํฌํ•จ, total count ์ฟผ๋ฆฌ ์ถ”๊ฐ€ ์‹คํ–‰ fetchCount() : count ์ฟผ๋ฆฌ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ count ์ˆ˜ ์กฐํšŒ Reference ์‹ค์ „! Querydsl - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜ Querydsl์˜ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค๋ฌด ํ™œ์šฉ๊นŒ์ง€, ํ•œ๋ฒˆ์— ํ•ด๊ฒฐํ•ด๋ณด์„ธ์š”!, ๋ณต์žกํ•œ ์ฟผ๋ฆฌ, ๋™์  ์ฟผ๋ฆฌ๋Š” ์ด์ œ ์•ˆ๋…•! Querydsl๋กœ ์ž๋ฐ” ๋ฐฑ์—”๋“œ..
QueryDSL์˜ ๊ธฐ๋ณธ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. @Test @DisplayName("๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ฟผ๋ฆฌ EX") fun testSearchQuery() { val eq: BooleanExpression = member.username.eq("member1") //username = "member1" val ne: BooleanExpression = member.username.ne("member1") //username != "member1" val eq_not: BooleanExpression = member.username.eq("member1").not() //username != "member1 val isNotNull: BooleanExpression = member.us..
Q-Type ์ฟผ๋ฆฌdsl์„ ์‚ฌ์šฉํ•˜๋ฉด QType์„ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. QType์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Qํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• new QMember("m") : ๋ณ„์นญ(alias)์„ ์ง์ ‘ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. JPQL ์‹คํ–‰ ์‹œ select m From Member m ~~ ์‹์œผ๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค. QMember.member : QueryDSL์ด ๊ธฐ๋ณธ์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค static-import๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ„์นญ์„ ์ง์ ‘ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ Joinํ•˜๋Š” ๊ฒฝ์šฐ ํ˜น์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณ„์นญ ์ง์ ‘ ์ง€์ • @Test @DisplayName("Qํƒ€์ž… ์ง์ ‘ ์ƒ์„ฑํ•˜๊ธฐ") f..
JPA ํ‘œ์ค€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ JPA์˜ ํ‘œ์ค€ ์˜ˆ์™ธ๋“ค์€ javax.persistence.PersistenceException์˜ ์ž์‹ ํด๋ž˜์Šค๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ˆ์™ธ ํด๋ž˜์Šค๋Š” RuntimeException์˜ ์ž์‹์ด๋‹ค. JPA ํ‘œ์ค€ ์˜ˆ์™ธ๋Š” ํฌ๊ฒŒ ๋ณด๋ฉด 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ์„ ํ‘œ์‹œํ•˜๋Š” ์˜ˆ์™ธ ํŠธ๋žœ์žญ์…˜ ์˜ˆ์™ธ๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š๋Š” ์˜ˆ์™ธ ํŠธ๋ž™์žญ์…˜ ๋กค๋ฐฑ์„ ํ‘œ์‹œํ•˜๋Š” ์˜ˆ์™ธ๋Š” ์‹ฌ๊ฐํ•œ ์˜ˆ์™ธ์ด๋ฏ€๋กœ ๋ณต๊ตฌํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค. ์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํŠธ๋žœ์žญ์…˜์„ ๊ฐ•์ œ๋กœ ์ปค๋ฐ‹ํ•ด๋„ ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹๋˜์ง€ ์•Š๊ณ  ๋Œ€์‹ ์— javax.persistence.RollbackException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋ฐ˜๋ฉด์— ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š๋Š” ์˜ˆ์™ธ๋Š” ์‹ฌ๊ฐํ•œ ์˜ˆ์™ธ๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ ํŠธ๋žœ์žญ์…˜์„ ์ปค๋ฐ‹ํ• ์ง€ ๋กค๋ฐฑํ• ์ง€๋ฅผ ํŒ๋‹จํ•œ๋‹ค. ์•„๋ž˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ถ„๋ฅ˜์˜ ์˜ˆ์™ธ๋ฅผ ํ‘œ..
๋ฆฌ์Šค๋„ˆ ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์–ธ์ œ ์–ด๋–ค ์‚ฌ์šฉ์ž๊ฐ€ ์‚ญ์ œ๋ฅผ ์š”์ฒญํ–ˆ๋Š”์ง€ ๋ชจ๋‘ ๋กœ๊ทธ๋กœ ๋‚จ๊ฒจ์•ผ ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด๋•Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‚ญ์ œ ๋กœ์ง์„ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ฐพ์•„๊ฐ€๋ฉฐ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์€ ๋„ˆ๋ฌด ๋น„ํšจ์œจ์ ์ด๋‹ค. ์ด๋Ÿด ๋•Œ JPA์˜ ๋ฆฌ์Šค๋„ˆ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด, ์—”ํ‹ฐํ‹ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋”ฐ๋ฅธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜ ์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜์™€ ๋ฐœ์ƒ ์‹œ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. PostLoad : ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์กฐํšŒ๋œ ์งํ›„, ๋˜๋Š” refresh๋ฅผ ํ˜ธ์ถœํ•œ ํ›„(2์ฐจ ์บ์‹œ์— ์ €์žฅ๋˜์–ด ์žˆ์–ด๋„ ํ˜ธ์ถœ๋œ๋‹ค.) PrePersist : persist() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ๊ด€๋ฆฌํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋œ๋‹ค. ์‹๋ณ„์ž ์ƒ์„ฑ ์ „๋žต์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ์—๋Š” ์—”ํ‹ฐํ‹ฐ์˜ ์‹๋ณ„์ž๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ์ด๋‹ค. ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ mer..
๋ง ๋ž‘
'๐Ÿ๏ธ Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (15 Page)