Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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 具有聚合内部联接的JPA条件查询_Java_Hibernate_Jpa_Criteria Api - Fatal编程技术网

Java 具有聚合内部联接的JPA条件查询

Java 具有聚合内部联接的JPA条件查询,java,hibernate,jpa,criteria-api,Java,Hibernate,Jpa,Criteria Api,我正在尝试编写一个标准查询,它将查询每个城市的最新观察结果。城市由城市代码字段定义,最新记录由观测时间字段定义 我可以很容易地用普通的SQL编写,但我不知道如何使用jpa标准api来编写 select distinct m.* from (select city_code cc, max(observation_time) mo from observations group by city_code) mx, observations m where m.city_code = mx

我正在尝试编写一个标准查询,它将查询每个城市的最新观察结果。城市由城市代码字段定义,最新记录由观测时间字段定义

我可以很容易地用普通的SQL编写,但我不知道如何使用jpa标准api来编写

select distinct m.* from 
 (select city_code cc, max(observation_time) mo
 from observations group by city_code) mx, observations m 
 where m.city_code = mx.cc and m.observation_time = mx.mo`

不幸的是,JPA不支持
FROM
子句中的子查询。您需要编写一个本机查询或使用类似的框架。

当您为松散的效率而开放时,这是可能的。 因此,首先让我们将查询转换为逻辑等价查询:

select distinct m.* from observations m where 
m.observation_time = (select max(inn. observation_time) from observations inn 
                      where inn.city_code = m.city_code);
然后我们将其转换为JPA CriteriaQuery:

public List<Observation> maxForEveryWithSubquery() {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Observation> query = builder.createQuery(Observation.class);
    Root<Observation> observation = query.from(Observation.class);
    query.select(observation);

    Subquery<LocalDateTime> subQuery = query.subquery(LocalDateTime.class);
    Root<Observation> observationInner = subQuery.from(Observation.class);
    subQuery.where(
            builder.equal(
                    observation.get(Observation_.cityCode),
                    observationInner.get(Observation_.cityCode)
            )
    );
    Subquery<LocalDateTime> subSelect = subQuery.select(builder.greatest(observationInner.get(Observation_.observationTime)));
    query.where(
            builder.equal(subSelect.getSelection(), observation.get(Observation_.observationTime))
    );
    TypedQuery<Observation> typedQuery = entityManager.createQuery(query);
    return typedQuery.getResultList();
}
公共列表maxForEveryWithSubquery(){
CriteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery=builder.createQuery(Observation.class);
根观察=query.from(observation.class);
查询。选择(观察);
Subquery Subquery=query.Subquery(LocalDateTime.class);
Root observationner=subQuery.from(Observation.class);
subQuery.where(
同等(
observation.get(observation\uu.cityCode),
observationiner.get(观测城市代码)
)
);
Subquery subSelect=Subquery.select(builder.magest(observationner.get(Observation.observationTime));
请问,在哪里(
builder.equal(subSelect.getSelection(),observation.get(observation\uuu.observationTime))
);
TypedQuery TypedQuery=entityManager.createQuery(查询);
返回typedQuery.getResultList();
}