如何在查询 select inner join many table in Spring Data JPA 时获取数据

How to get data when query select inner join many table in Spring Data JPA

这是我的项目。我有问题,我想从 table 获取数据 Student join many table branch, room, and gender use annotation @Query as site query in the image MySql

This is query with statement MySql

This is code I want to use @Query in Spring Data JPA with purpose get data when join many table

调用API后,报错 This is error

我需要支持,这是我的项目: https://github.com/daovantam0410/quan-ly-dao-tao

你有两个选择,你可以使用从数据库中获取学生。

List<Student> students = studentRepository.findAll();

这将通过发出其他查询来加载其他实体(房间、性别和分支),这将导致著名的问题 N+1 queries

因此,要一次加载所有实体,请使用 entity graphsfetch join

@Query(value = "SELECT st FROM Student st INNER JOIN FETCH st.branch b INNER JOIN FETCH st.gender g INNER JOIN FETCH st.room r")
List<Student> fetchStudentDataJoinTable();

注意:如果属性(roomgenderbranch)在student中可以是null则使用left join 而不是 inner join.