从SQL查询到JPA查询

时间:2012-07-04 14:46:40

标签: java jpa

我如何将其转换为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());

0 个答案:

没有答案