【问题标题】:Spring repository how to find in list based on field's id?Spring存储库如何根据字段的ID在列表中查找?
【发布时间】:2021-12-04 10:09:36
【问题描述】:

我有一个包含两组玩家的游戏对象:白人贡献者和黑人贡献者。我希望能够根据玩家对象的 id 字段而不是玩家对象本身来找到在白色或黑色贡献者集中有玩家的游戏。您可以纯粹通过存储库方法的命名约定来做到这一点吗?这是上下文的一些代码:

游戏回购:

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

游戏对象:

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

玩家对象:

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

【问题讨论】:

    标签: java spring jpa spring-data-jpa spring-data


    【解决方案1】:

    我发现如果我像这样使用 2 个参数,我可以做到这一点

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

    如果我能找到任何一个集合同时只取 1 个参数就更好了。但目前看来这行得通?

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多