繁体   English   中英

如何在sql中使用基于字段的单个子对象查询表中的对象?

[英]How to I query sql for a Object in a Table with a single sub-object based on a field?

我现在所拥有的是:

获取所有社区等的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM