简体   繁体   中英

How do I do a JPA findAllBy native query for an object's property value when that object is within a List object, within an outer object?

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!

Try using join fetch. Refer below link for more details.

https://dzone.com/articles/how-to-decide-between-join-and-join-fetch

Try this query,

Query(value ="SELECT * FROM user u INNER JOIN userStats us ON u.id = us.user_id where (u.location = :location AND u.rating BETWEEN :lowerRatingBound AND :upperRatingBound) AND (us.victoryScore BETWEEN :lowerVictoryScoreBound AND :upperVictoryScoreBound)",nativeQuery = true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM