简体   繁体   English

Spring 存储库 findBy 连接表

[英]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.

相关问题 使用 spring 的 Redis 存储库中的 FindBy - FindBy in Redis Repository with spring Spring 引导应用程序中的 Jpa 存储库按问题查找 - Jpa Repository in Spring boot app findBy issue Spring Data JPA:连接列上的 FindBy - Spring Data JPA: FindBy on Join Column Spring 启动,@ManyToMany,为加入表创建存储库 - Spring boot, @ManyToMany, create a repository for the join Table 如何在Spring Boot中使存储库findBy()具有多个字段? - How to make repository findBy() with more than one field in Spring Boot? 如何使用findBy Spring存储库在Java中的Map中检索数据 - How to retrieve data within a Map in java using findBy spring repository 如何为findBy Join table列制定JPA方法 - How to formulate JPA method for findBy Join table column Spring JPA存储库findBy直接通过外键使用POJO代替类的数据成员 - Spring JPA Repository findBy foreign key directly with POJO instead of data member of class 如何使用包含关键字的属性创建 Spring JPA 存储库 findBy 查询? - How do you create a Spring JPA repository findBy query using a property that contains a keyword? Spring存储库找不到`findBy*`方法,将事务标记为回滚,导致持久化失败 - Spring repository does not find a `findBy*` method and marks the transaction for rollback, causing persistence failures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM