【问题标题】:How to I query sql for a Object in a Table with a single sub-object based on a field?如何使用基于字段的单个子对象在表中查询 sql 中的对象?
【发布时间】:2018-08-08 11:53:39
【问题描述】:

我现在拥有的是:

获取所有社区等的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的第一个社区。​​p>

我该怎么做?

我不想把它们全部拿走,然后再删除。

谢谢。

【问题讨论】:

    标签: java mysql hibernate jpa entitymanager


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2012-02-05
      • 1970-01-01
      • 2018-12-15
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多