// 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
Specify a DTO class and pass parameters of the constructor to selectNew() to build a DTO projection.
data class Row(
val departmentId: Long,
val count: Long,
)
selectNew<Row>(
path(EmployeeDepartment::departmentId),
count(Employee::employeeId),
)
Use join() and fetchJoin() to combine the entities for selection. There are two types of join(): Join and Association Join This is distinguished by whether join() is used between two unrelated entities or between two related entities.
@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
)
Call as() after join() to alias the entity being joined. This can be useful if you use multiple entities with the same type in a from clause.
Use select() and pass to build a select clause in the select statement. If you pass only one Expression to select(), it will infer a return type from Expression. However, if you pass more than one Expression, you need to specify the type as it cannot infer the type.
Use from() and pass and to specify the entities for selection when building a from clause in the select statement.
Use where() and pass to restrict the data when building a where clause in the select statement. You can use whereAnd() as a short form of where() and and(). You can also use whereOr() as a short form of where() and or().
Use groupBy() and pass to create unique groups of data when building a group by clause in the select statement.
Use having() and pass to further restrict the data when building a having clause in the select statement. You can use havingAnd() as a short form of having() and and(). You can also use havingOr() as a short form of having() and or().
Use orderBy() and pass to return data in the declared order when building an order by clause in the select statement.
Use update() and pass to specify the entity to modify when building an update clause in the update statement.
Use set() and pass to assign values when building a set clause in the update statement. You can use multiple assignments by adding set() after set().
Use where() and pass to restrict the data when building a where clause in the update statement. You can use whereAnd() as a short form of where() and and(). You can also use whereOr() as a short form of where() and or().
Use deleteFrom() and pass to specify the entity to delete when building a delete from clause in the delete statement.
Use where() and pass to restrict the data when building a where clause in the delete statement. You can use whereAnd() as a short form of where() and and(). You can also use whereOr() as a short form of where() and or().