[英]Spring repository findBy join table
I have the following models我有以下型号
GameModel游戏模型
// ...
@ManyToMany(cascade = CascadeType.DETACH)
@JoinTable(
name = "game_genre",
joinColumns = { @JoinColumn(name = "game_id") },
inverseJoinColumns = { @JoinColumn(name = "genre_id") } )
private List<GenreModel> genres;
// ...
GenreModel流派模型
// ...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name", nullable = false, unique = true, length = 50)
private String name;
@ManyToMany(mappedBy = "genres")
private List<GameModel> games;
// ...
Also, I have repositories for them.另外,我有他们的存储库。
How can I get all the games that belong to (for example) genre with id 1 ?我怎样才能获得所有属于(例如) id 为1 的类型的游戏? In the GameModel , I have no genres column, so I can not do it in the GameRepository using findByGenresContaining.在GameModel 中,我没有流派列,因此我无法在GameRepository 中使用 findByGenresContaining 进行此操作。
Thanks to @billalGHILAS It works like this:感谢@billalGHILAS它是这样工作的:
// find games that belong to one genre
public List<GameModel> findByGenresId(int id);
// find games that belong to list of genres
public List<GameModel> findByGenresIdIn(List<Integer> id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.