2017-01-26 202 views
0

我使用Spring的数据(JPA的存储库)中选择,我有以下实体:JPQL通过属性列表

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. 

我怎么能这样做?

+0

我对JPQL并不熟悉。是否事实上,建筑物(和单位)是'列表' – Michael

+0

@迈克尔 - 我敢肯定 - 我只是不知道什么是正确的查询(我确信有一种方法) – Noam

+0

@Gimby - 感谢您注意到错字(改变了我的问题) - 仍然得到这个错误 – Noam

回答

-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只要告诉我:)。

+0

你能把它转换成JPQL ...吗?并可以使用连接来完成?什么更好? – Noam