[英]JPQL query with many-to-many relationship
We have 2 entities Technology and Project with a Many-to-Many relationship, which are linked with an additional reference table.我们有 2 个实体 Technology 和 Project 具有多对多关系,它们与附加的参考表链接。
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
I can retreive Technology from db with such a query:我可以使用这样的查询从 db 中检索技术:
@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.必须使用带有left join fetch
子句的JPQL 查询来检索具有项目集合的技术,以避免延迟初始化异常。
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). (查询findAllByProject(10)
必须返回技术 1000 和 2000)。
I cannot use native SQL query here because I need join fetch
to get collection of projects.我不能在这里使用本机 SQL 查询,因为我需要join fetch
来获取项目集合。
By adding a where clause on project entity.通过在项目实体上添加 where 子句。
@Query("select t from Technology t left join fetch t.projects p where p.id=10")
List<Technology> findAllByProject10();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.