我在内部查询中编写了Distinct的JPA代码,下面是我的JPA代码
CriteriaBuilder cb = entityManager_gbl.getCriteriaBuilder();
CriteriaQuery<sourceTracking> cq = cb.createQuery(sourceTracking.class);
Root<sourceTracking> data1 = cq.from(sourceTracking.class);
Subquery<String> subq=cq.subquery(String.class);
Root<sourceTracking> sbf=subq.from(sourceTracking.class);
subq.select(sbf.<String>get("message_id")).distinct(true);
cq.where(data1.<String>get("tablekey").in(subq));
cq.select(data1);
此代码生成SQL查询
select *
from source_tracking
where tablekey in (select distinct message_id from source_tracking )
但是我的要求在这里,distinct应用于message_id,但是获取tablekey。
select *
from source_tracking
where tablekey in (select distinct on (message_id) tablekey
from source_tracking)