728x90
์ ๋ ฌ
์ ๋ ฌ์ 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 = teamA),
Member(username = "member3", age = 30, team = teamB),
Member(username = "member4", age = 40, team = teamB),
Member(username = "member5", age = 50, team = teamA),
Member(username = "member6", age = 60, team = teamA),
Member(username = "member7", age = 70, team = teamB),
Member(username = "member8", age = 80, team = teamB),
Member(username = "member9", age = 90, team = teamA),
Member(username = "member10", age = 100, team = teamA),
Member(username = "member11", age = 110, team = teamB),
Member(username = "member12", age = 120, team = teamB),
)
members.forEach { em.persist(it) }
em.flush()
em.clear()
}
์์
/**
* 1. ํ์ ๋์ด ๋ด๋ฆผ์ฐจ์
* 2. ํ์ ์ด๋ฆ ์ค๋ฆ์ฐจ์
* ๋จ 2์์ ํ์ ์ด๋ฆ์ด ์์ผ๋ฉด(๋น ๋ฌธ์์ด์ด ์๋ ์ง์ง null) ๋ง์ง๋ง์ ์ถ๋ ฅ
*/
@Test
@DisplayName("์ ๋ ฌ")
fun testSort() {
//given
em.persist(Member(username = null, age = 100 ))
em.persist(Member(username = "", age = 100 ))
em.persist(Member(username = "member6", age = 100 ))
em.persist(Member(username = "member7", age = 100 ))
//when
val fetch = query.selectFrom(member)
.where(member.age.eq(100))
.orderBy(
member.age.desc(),
member.username.asc().nullsLast()
)
.fetch()
//then
val memberEmptyUsername = fetch[0]
val member6 = fetch[1]
val member7 = fetch[2]
val memberNull = fetch[3]
assertThat(memberEmptyUsername.username).isEqualTo("")
assertThat(memberEmptyUsername.username).isEmpty()
assertThat(member6.username).isEqualTo("member6")
assertThat(member7.username).isEqualTo("member7")
assertThat(memberNull.username).isNull()
}
Reference
728x90
'๐๏ธ Spring > QueryDsl' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[QueryDSL] GroupBy (0) | 2021.12.23 |
---|---|
[QueryDSL] ์ง๊ณ ํจ์(count, sum, avg, max, min) (0) | 2021.12.23 |
[QueryDSL] ๊ฒฐ๊ณผ ์กฐํ (0) | 2021.12.23 |
[QueryDSL] ๊ฒ์ ์กฐ๊ฑด ์ฟผ๋ฆฌ (3) | 2021.12.23 |
[QueryDSL] Q-Type ์ฌ์ฉ๋ฒ (0) | 2021.12.23 |