【问题标题】:Query ManyToMany SpringBoot查询多对多 Spring Boot
【发布时间】:2018-10-29 06:12:10
【问题描述】:

我有以下建模:

table hackathon
idEvent int pk ai
...


table team
idTeam int pk ai
...

table hackathon_has_team
hackathon_id int
team_id int
....

而且我需要按事件 ID 选择参加某个事件的所有团队,而我对此感到头疼,有人可以帮忙吗?

我正在尝试这样做,但它不起作用:

@Query("SELECT t " 
    + "FROM Team t INNER JOIN hackathon_has_team " 
    + "ON hackathon_has_team.team_id = t.id " 
    + "WHERE hackathon_has_team.hackathon_id = :hackathon_id")
public Page<Team> getListAllTeamsByIdOfHackathon(@Param("hackathon_id ") Long id, Pageable pageable);

【问题讨论】:

  • 编辑问题添加一些示例数据,期望的结果会有所帮助。

标签: spring hibernate spring-boot jpa many-to-many


【解决方案1】:

您可能需要提供更多才能获得更多,但根据您获取团队列表的方式,您可以尝试:

select * from event_has_team
where (select team_id from table team) 

这会从 event_has_team 表中选择 team_id 在 team 表中的所有内容。您还可以为您在该子选择或主选择中确定的团队添加 where 子句,具体取决于您的实际需要。

【讨论】:

  • 不是这样的
猜你喜欢
  • 2020-03-29
  • 2021-04-13
  • 2023-03-17
  • 2018-01-28
  • 2020-08-02
  • 2017-10-28
  • 2016-01-31
  • 2018-10-31
  • 2016-06-03
相关资源
最近更新 更多