Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从SQL查询到JPA查询_Java_Jpa - Fatal编程技术网

Java 从SQL查询到JPA查询

Java 从SQL查询到JPA查询,java,jpa,Java,Jpa,如何将其转换为jpa/jpl查询?是否可以/更干净地使用标准查询编写它 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 n

如何将其转换为jpa/jpl查询?是否可以/更干净地使用标准查询编写它

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());

+1了解此类复杂查询的条件。请向我们展示您自己在尝试这样做方面做了一些努力。这里没有人愿意为你工作。如果您尝试了某个方法但无效,请向我们展示,我们可以帮助您解决特定问题。我认为问题可能在于我将
setParameter(String
setParameter)混合使用(int
仅作为记录,您可以在初始查询中使用联接而不是笛卡尔乘法。我只能以sql的方式使其工作。我仍在试图理解CriteriaQuery在这种特殊情况下如何工作