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;
}