I have the following table on database:
id | employee_id | title | created_at |
---|---|---|---|
1 | 10 | title1 | 2022-10-10 |
1 | 10 | title2 | 2022-10-11 |
I want fetch data by distinct on title column;
by sql I can do this:
select distinct on (employee_id) *
from contracts
order by employee_id, created_at desc
The size of result is one and it is correct:
id | employee_id | title | created_at |
---|---|---|---|
1 | 10 | title2 | 2022-10-11 |
But when I want implement this by psecification
, cannot distinct
result by column:
Specification { root: Root<Contract>, query: CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder ->
val firstNamePredicate: Predicate =
criteriaBuilder.equal(root.get<String>("employee").get<String>("id"), "1")
query.distinct(true)
criteriaBuilder.and(firstNamePredicate)
}
This query return all rows that employee_id
is equals 1.
I applied below changes, but result not changed:
query.select(root.get<String>("employee").get("id")).distinct(true)
How can I apply distinct on column?