I am trying to code the matchmaking feature of my game web-app, the User entity contains a property, UserStats with a OneToMany relationship which stores a list of 2 UserStat JPA entitites, these userStat entities have 2 different sets of stats, and one of the stats I require is called victoryScore.
How can I search for Users where their UserStats.get(0).victoryScore is a specific value/range of values?
I think I need to use JOIN possibly, here is my attempt:
@Query(
value = "SELECT * FROM user u JOIN u.userStats us WHERE u.location = :location AND u.rating BETWEEN :lowerRatingBound AND :upperRatingBound AND us.victoryScore BETWEEN :lowerVictoryScoreBound AND :upperVictoryScoreBound",
nativeQuery = true)
List<User> findChallengingUsers(@Param("lowerRatingBound") Integer lowerRatingBound, @Param("upperRatingBound") Integer upperRatingBound, @Param("lowerVictoryScoreBound") Double lowerVictoryScoreBound, @Param("upperVictoryScoreBound") Double upperVictoryScoreBound, @Param("location") String location);
And the error:
No property victoryScore found for type User!
User:
@Entity(name = "User")
@Table(name = "user")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "username", unique = true)
private String username;
@Column(name = "password")
private String password;
private String name;
private String email;
private String location;
private String bio;
private Integer rating;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.PERSIST)
private List<UserStats> userStats;
}
UserStats:
@Entity()
@Table(name = "userStats")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserStats {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userStatsId;
private Integer victories;
private Integer draws;
private Integer defeats;
private Double victoryScore;
private boolean isLeagueUserStats;
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(name = "user_id")
private User user;
}
Is this even possible?
For reference, I access the victoryScore value for how I want it to search for using:
user.getUserStats(0).getVictoryScore;
The user stats class is also a domain entity so also has its own repository interface.
Any help would be greatly appreciated, thanks!