Springboot jpa,使用两个连接获取具有特定字段的行

时间:2018-04-12 15:55:21

标签: spring-boot jpa spring-data-jpa

我有实体:StudentClassRoom

student M-M classstudent M-M rooms,因此有两个联结表。现在,我希望将所有students分配到课程X和房间Y。因此我做了:

@Query("SELECT s from Student s INNER JOIN s.classes c INNER JOIN s.rooms r WHERE c.id LIKE ?1 AND r.id LIKE ?2")
Page<Student> findAllInClassAndRoom(final Long classId, final Long roomId, final Pageable pageable); 

但它给了我错误的结果。我的查询中是否有错误?

1 个答案:

答案 0 :(得分:1)

查询中唯一的错误是LIKE语句。只需更改等号&#34; =&#34;。如下:

@Query("SELECT s from Student s INNER JOIN s.classes c INNER JOIN s.rooms r WHERE c.id = ?1 AND r.id = ?2")
Page<Student> findAllInClassAndRoom(final Long classId, final Long roomId, final Pageable pageable); 

LIKE语句允许更大量的数据。因为它允许任何Room或Class代码的一部分是作为参数输入的id。 LIKE statement specification