Spring repository how to find in list based on field's id?

Viewed 24

I have a game object which holds 2 sets of players: white contributors and black contributors. I want to be able to find games that have a player in either the white or black contributor sets but based on the id field of the player object rather than the player object itself. Can you do this purely by the naming convention of the repository's method? Here's some code for context:

Game Repo:

@Repository
public interface GameRepository extends JpaRepository<Game, Integer> {
    Page<Game> findByPlayer_IdInWhiteContributorsOrPlayer_IdInBlackContributors(final int id, Pageable pageable);
}

Game Object:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "game")
public class Game {

    @Transient
    public static final String STARTING_POSITION_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String pdn;

    @Length(min = 0, max = 255)
    private String fen = STARTING_POSITION_FEN;

    @Column(name = "white_contributors")
    @ManyToMany(mappedBy = "gamesAsWhite")
    private Set<Player> whiteContributors;

    @Column(name = "black_contributors")
    @ManyToMany(mappedBy = "gamesAsBlack")
    private Set<Player> blackContributors;
    
    @CreationTimestamp
    private Timestamp created;
    
    @UpdateTimestamp
    private Timestamp lastUpdated;
}

Player Object:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "player")
public class Player {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "display_name")
    @Length(min = 1, max = 25)
    private String displayName;

    @Email
    @Length(min = 5, max = 255)
    private String email;

    private String password;

    @Length(max = 64)
    private String name;

    @Length(max = 255)
    @Nullable
    private String about;

    @Nullable
    private Date birthday;

    @Column(name = "games_as_white")
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "player_games_as_white", joinColumns = { @JoinColumn(name = "id") })
    private Set<Game> gamesAsWhite;

    @Column(name = "games_as_black")
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "player_games_as_black", joinColumns = { @JoinColumn(name = "id") })
    private Set<Game> gamesAsBlack;

    @PositiveOrZero
    private int rating;

    @PositiveOrZero
    private int wins;

    @PositiveOrZero
    private int losses;

    @PositiveOrZero
    private int draws;
}
1 Answers

I found I can do this if I use 2 parameters like so

@Repository
public interface GameRepository extends JpaRepository<Game, Integer> {
    Page<Game> findByWhiteContributorsOrBlackContributors(final Player playerWhite, final Player playerBlack, Pageable pageable);
    Page<Game> findByWhiteContributors_IdOrBlackContributors_Id(final int idWhite, final int idBlack, Pageable pageable);
}

It would be better if I could find by either set while only take 1 parameter. But for the time being this seems to work?

Related