How to select all distinct records as entities - hibernate - JPA?

I want to select all distinct records not a specific one and as entities.

I’m using Hibernate 5.6.15, JPA 2.2 and JDK 11 LTS

I tried it out though I know it’s a not proper query.

public List<Orders> custOrder() {
    try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
        CriteriaBuilder cb = session.getCriteriaBuilder();
        CriteriaQuery<Orders> cq = cb.createQuery(Orders.class);
        Root<Orders> root = cq.from(Orders.class);"transid")).distinct(true);

        return session.createQuery(cq).getResultList();

So I want to select all distinct transid(s) but as entities not as String.
So how to do?
Kindly help.

It’s not so simple. If you have two different entities with the same transid, which entity do you want to return? You can write a HQL query like the following if you don’t care about which one:

select o
from Orders o
where in (
  select max(
  from Orders o2
  group by o2.transid

Thank you beikov.
That’s what I was looking for.
Have a nice time :slightly_smiling_face: