JPQL์ select, update, delete statement๋ฅผ ์ง์ํฉ๋๋ค. Kotlin JDSL์ ์ด statement๋ค์ ๋ง๋ค ์ ์๋ DSL์ ์ ๊ณตํฉ๋๋ค.
Select statement
jpql()
์์ select()
๋ฅผ ํธ์ถํ๋ ๊ฒ์ผ๋ก select statement๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
Copy val query = jpql {
select(
path(Employee::employeeId),
).from(
entity(Employee::class),
join(Employee::departments),
).where(
type(entity(Employee::class)).eq(FullTimeEmployee::class)
).groupBy(
path(Employee::employeeId),
).having(
count(Employee::employeeId).greaterThan(1L),
).orderBy(
count(Employee::employeeId).desc(),
path(Employee::employeeId).asc(),
)
}
Select clause
Copy // It can infer the result type.
select(path(Author::authorId))
// It cannot infer the result type.
select(path(Author::authorId), path(Author::name))
// This allows it to know the result type.
select<CustomEntity>(path(Author::authorId), path(Author::name))
DTO projection
DTO ํด๋์ค์ ํด๋์ค์ ์์ฑ์๋ฅผ selectNew()
์ ๋๊ธฐ๋ ๊ฒ์ผ๋ก DTO ํ๋ก์ ์
์ ๋ง๋ค ์ ์์ต๋๋ค.
Copy data class Row(
val departmentId: Long,
val count: Long,
)
selectNew<Row>(
path(EmployeeDepartment::departmentId),
count(Employee::employeeId),
)
From clause
Copy from(
entity(Author::class),
join(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
)
Join
์กฐํ๋๋ entity๋ฅผ ์กฐ์ธํ๊ธฐ ์ํด, join()
๊ณผ fetchJoin()
์ ์ฌ์ฉํ ์ ์์ต๋๋ค. Join์๋ 2์ข
๋ฅ๊ฐ ์์ผ๋ฉฐ ์ผ๋ฐ Join๊ณผ ์ฐ๊ด๊ด๊ณ Join์ด ์์ต๋๋ค. ๋ Join์ ์ฐ๊ด๊ด๊ณ๊ฐ ์๋ entity๋ฅผ ์กฐ์ธํ๋์ง ์๋ entity๋ฅผ ์กฐ์ธํ๋์ง์ ๋ฐ๋ผ ๊ตฌ๋ณ๋ฉ๋๋ค.
Copy @Entity
// ...
class Book(
// ...
@OneToMany(mappedBy = "book", cascade = [CascadeType.ALL], orphanRemoval = true)
val authors: MutableSet<BookAuthor>,
)
@Entity
// ...
class BookAuthor(
@Id
@Column(name = "author_id")
val authorId: Long,
) {
@Id
@ManyToOne
@JoinColumn(name = "isbn")
lateinit var book: Book
}
@Entity
// ...
class Author(
@Id
@Column(name = "author_id")
val authorId: Long,
// ...
)
from(
entity(Book::class),
join(Book::authors), // Association Join
join(Author::class).on(path(BookAuthor::authorId).eq(path(Author::authorId))), // Join
)
join()
์ดํ์ as()
๋ฅผ ํธ์ถํ๋ ๊ฒ์ผ๋ก ์กฐ์ธ๋ entity์ alias๋ฅผ ๋ถ๊ฐํ ์ ์์ต๋๋ค. ๋ง์ฝ ๋์ผํ ํ์
์ entity๋ฅผ ์ฌ๋ฌ๊ฐ from clause์ ํฌํจ์ํฌ ๋ ์ด ๊ธฐ๋ฅ์ ์ด์ฉํ ์ ์์ต๋๋ค.
Copy from(
entity(Book::class),
join(Book::authors).`as`(entity(BookAuthor::class, "author")),
)
Where clause
Copy where(
path(Book::publishDate).between(
OffsetDateTime.parse("2023-01-01T00:00:00+09:00"),
OffsetDateTime.parse("2023-06-30T23:59:59+09:00"),
),
)
Group by clause
Copy groupBy(
path(EmployeeDepartment::departmentId),
)
Having clause
Copy having(
count(Employee::employeeId).greaterThan(1L),
)
Order by clause
Copy orderBy(
path(Book::isbn).asc(),
)
Update statement
jpql()
์์ update()
๋ฅผ ํธ์ถํ๋ ๊ฒ์ผ๋ก update statement๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
Copy val query = jpql {
update(
entity(Book::class)
).set(
path(Book::price)(BookPrice::value),
BigDecimal.valueOf(100)
).set(
path(Book::salePrice)(BookPrice::value),
BigDecimal.valueOf(80)
).where(
path(Book::isbn).eq(Isbn("01"))
)
}
Update clause
Copy update(
entity(Employee::class),
)
Set clause
Copy set(
path(Book::price)(BookPrice::value),
BigDecimal.valueOf(100)
).set(
path(Book::salePrice)(BookPrice::value),
BigDecimal.valueOf(80)
)
Where clause
Copy where(
path(Book::publishDate).between(
OffsetDateTime.parse("2023-01-01T00:00:00+09:00"),
OffsetDateTime.parse("2023-06-30T23:59:59+09:00"),
),
)
Delete statement
jpql()
์์ deleteFrom()
๋ฅผ ํธ์ถํ๋ ๊ฒ์ผ๋ก delete statement๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
Copy val query = jpql {
deleteFrom(
entity(Book::class),
).where(
path(Book::publishDate).ge(OffsetDateTime.parse("2023-06-01T00:00:00+09:00")),
)
}
Delete from clause
Copy deleteFrom(
entity(Book::class),
)
Where clause
Copy where(
path(Book::publishDate).between(
OffsetDateTime.parse("2023-01-01T00:00:00+09:00"),
OffsetDateTime.parse("2023-06-30T23:59:59+09:00"),
),
)