0

我如何将其转换为 jpa/jpl 查询?使用CriteriaQuery编写它是否可能/更干净?

select vc.category_id, count(distinct vc.something_id)
from 
  place p, something_category vc, something_poi vp, something v left join something_external_provider vep on vep.something_id=v.id
and (vep.provider is null or (vep.provider = 'EULA' and vep.lastretrieveddate >= now()-interval 1 day))
where p.id=vp.place_id and 
v.id=vc.something_id and
v.id=vp.something_id and 
v.isprivate=0 and 
(v.is_sponsored is null OR v.is_sponsored=0) and 
v.status in (1,11,31) and 
vp.status in (1,2) and 
(EXISTS (select id from something_app where something_id=v.id and app_id=1) OR NOT EXISTS (select id from something_app where something_id=v.id)) and 
v.enddate>now() and 
(country='US' and type='country,political')
group by vc.category_id;

这是我到目前为止的结果

private static final String QUERY = "SELECT vc.category, COUNT(DISTINCT vc.something) "
        + "FROM Place p, SomethingCategory vc, SomethingPoi vp, Something v LEFT JOIN SomethingExternalProvider vep ON vep.something=v AND "
        + "(vep.provider IS null or (vep.provider in ( :providers ) and vep.lastretrieveddate >= (now() - INTERVAL 1 DAY))) "
        + "WHERE p=vp.place AND v=vc.something AND v=vp.something AND v.isprivate=0 AND "
        + "(v.isSponsored is null OR v.isSponsored=0) AND v.status IN (?,?,?)) AND vp.status IN (1,2) and "
        + "(EXISTS (SELECT vga.id FROM SomethingApp vga WHERE something = v AND app = :app) OR NOT EXISTS (SELECT vga.id FROM SomethingApp vga WHERE something=v)) AND "
        + "v.enddate>now() AND (country = :country AND type='country, political') " + "GROUP BY vc.category";

接着

    Query query = entityManager.createQuery(QUERY).setParameter("providers", string)
        .setParameter("app", app).setParameter("US", country)
        .setParameter(1, SomethingStatusType.ACTIVE.getType())
        .setParameter(2, SomethingStatusType.EXTERNAL.getType());
4

0 回答 0