【问题标题】:JPQL select by attribute in list [duplicate]JPQL按列表中的属性选择[重复]
【发布时间】:2023-04-07 09:10:02
【问题描述】:

我正在使用 Spring 数据 (jpa-repository),并且我有以下实体:

public class City{
    ....
    private Street street;
    ....
}

public class Street{
    ...
    private List<Building> buildings;
    ...
}

public class Building{
    ... 
    private List<Flat> flats;
    ....
}

public class Flat{
   ...
   private boolean lightsOn;
   ...
}

我想创建一个查询来获取所有(不同的)城市,这些城市至少有一个带电灯的公寓。

我试过这个查询:

@Query("select distinct c from Cities c where  c.street.buildings.flats.lightsOn = true")

但收到此错误消息:

The state field path 'c.street.buildings.flats.lightsOn' cannot be resolved to a valid type.

我该怎么做?

【问题讨论】:

  • 我不熟悉 JPQL。建筑物(和公寓)是Lists
  • @Michael - 我确定是这样 - 我只是不知道正确的查询是什么(我确定有办法)
  • @Gimby - 感谢您注意到错字(更改了我的问题) - 仍然收到此错误
  • @Gimby - 再次感谢
  • select distinct c from City c JOIN c.street s JOIN s.buildings b JOIN b.flats f where f.lightsOn = true。但是看任何 JPQL 教程都会告诉你如何加入!

标签: java sql jpa spring-data jpql


【解决方案1】:

这是 sql 代码:

select * from city as c where c.idcity in 
    (select s.idcity from street as s where c.idcity = s.idcity and s.idstreet in
        (select b.idstreet from building as b where b.idstreet = s.idstreet and b.idbuilding in
            (select f.idbuilding from flat as f where f.idbuilding = b.idbuilding and f.lightsOn = 1)
        )
    )

如果您发现将其转换为 JPQL 有困难,请告诉我 :) 。

【讨论】:

  • 能否将其转换为 JPQL...?可以使用join来完成吗?什么更好?
猜你喜欢
  • 2018-08-29
  • 1970-01-01
  • 2015-01-05
  • 2014-07-06
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
相关资源
最近更新 更多