JPQL按列表中的属性选择

时间:2017-01-26 16:29:18

标签: java sql jpa spring-data jpql

我使用的是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.

我该怎么做?

1 个答案:

答案 0 :(得分:-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有困难请告诉我:)。