我现在所拥有的是:

获取所有社区等的List<Town>

(Town) em.createQuery(from Town towns"
        + " left join fetch towns.neighborhoods neighborhood"
        + " left join fetch neighborhood.population population"
        + " left join fetch population.age age"
        + " where age.value = :ageValue")
       .setParameter("ageValue", ageValue)


我想得到一个只有一个邻居的List<Town>,这是第一个在neighborhood.creationDate的邻居。

我怎么做 ?

我不想全部拿走然后将其删除。

谢谢。

最佳答案

我建议在where子句中添加一个子查询,如下所示:

where age.value = :ageValue
    AND neighborhood.creationDate =
         (Select max(nh.creationDate) From Neighborhood nh
               where nh.townsId = towns.townsId)

09-27 15:45