具有多对多关系的JPQL查询

我们有两个具有多对多关系的技术实体和项目实体,它们相互关联 以及其他参考表。

technologies
 id      name
1000 | digging
2000 | drilling

projects
id    name
10 | London
20 | Madrid

technologies_projects
tech_id     project_id
1000     | 10
2000     | 10
1000     | 20

我可以通过这样的查询从数据库检索技术:

@Query("select t from Technology t left join fetch t.projects")
List<Technology> findAll();

JPQL query with left join fetch clause must be used to retreive Technology with collection of projects to avoid lazy initialization exception.

The question is: How must the query be modified do get the list of technologies, used in a certain project? (the query findAllByProject(10) must return technologies 1000 and 2000).

I cannot use native SQL query here because I need join fetch to get collection of projects.

评论
uautem
uautem

通过在项目实体上添加where子句。

@Query("select t from Technology t left join fetch t.projects p where p.id=10")
List<Technology> findAllByProject10();
点赞
评论