【问题标题】:Springboot jpa, get rows with specific fields using two joinsSpring Boot jpa,使用两个连接获取具有特定字段的行
【发布时间】:2018-04-12 15:55:21
【问题描述】:

我有实体: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); 

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

【问题讨论】:

  • 我认为你在按id搜索时不应该使用LIKE

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


【解决方案1】:

查询中的唯一错误是 LIKE 语句。只需更改等号“=”。如下:

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多