spring数据:jpa存储库返回map而不是list?

r7s23pms  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(293)

我有这样的问题,我想从哪里提取电影标题和评级

@Query(value ="SELECT m.title, avg(r.rating) " +
        "FROM movies m " +
        "INNER JOIN " +
        "ratings r " +
        "ON m.movieid = r.movieid " +
        "WHERE m.genres LIKE %:genre% " +
        "GROUP BY m.title " +
        "ORDER BY avg(r.rating) DESC " +
        "LIMIT 10",
        nativeQuery = true)
 List<Movies> topTenMoviesByGenreLikeIgnoreCase(@Param("genre") String genre);

但是这个列表显然不能存储有2个值的结果集
在了解了它之后,我发现这个链接spring data:jpa repository findall()返回*map而不是list?但在我看来,它们的实施似乎并不清楚。
我不明白他们在第一个环节从哪里得到的长期价值

default Map<Long, TransactionModel> findByClientIdMap(Long id) {
    return findByClientId(id).stream().collect(Collectors.toMap(TransactionModel::getId, v -> v));
}

如何创建Map?

default Map<Movies, Float> topTenMoviesByGenreLikeIgnoreCaseMap(@Param("genre") String genre) {
    return topTenMoviesByGenreLikeIgnoreCase(genre).stream().collect(Collectors.toMap());
}

电影课:

@Entity
public class Movies {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer movieid;

private String title;
private String genres;

@OneToMany(mappedBy = "movieid")
private Set<Ratings> rates = new HashSet<>();

public Movies() {
}

public Movies(String title, String genres) {
    this.title = title;
    this.genres = genres;
}

/*Getters and Setters for variables*/

public Set<Ratings> getRates() {
    return rates;
}

public void setRates(Set<Ratings> rates) {
    this.rates = rates;
}
}

评级等级:

@Entity
public class Ratings {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer rateid;

@ManyToOne
@JoinColumn(name = "movieid")
private Movies movieid;

@ManyToOne
@JoinColumn(name = "userid")
private Usrs userid;

private float rating;
private Integer timestamp;

public Ratings() {
}

public Ratings(Movies movieid, Usrs userid, float rating, Integer timestamp)
{
    this.movieid = movieid;
    this.userid = userid;
    this.rating = rating;
    this.timestamp = timestamp;
}
/*Getters and Setters for variables*/
}
vxqlmq5t

vxqlmq5t1#

你可以用 Object[] 以此方式作为查询结果

@Query(value ="SELECT m.title, avg(r.rating) " +
        "FROM movies m " +
        "INNER JOIN " +
        "ratings r " +
        "ON m.movieid = r.movieid " +
        "WHERE m.genres LIKE %:genre% " +
        "GROUP BY m.title " +
        "ORDER BY avg(r.rating) DESC " +
        "LIMIT 10",
        nativeQuery = true)
 List<Object[]> topTenMoviesByGenreLikeIgnoreCase(@Param("genre") String genre);

那么呢

default Map<String, Float> topTenMoviesByGenreLikeIgnoreCaseMap(@Param("genre") String genre) {
    return topTenMoviesByGenreLikeIgnoreCase(genre)
            .stream()
            .collect(Collectors.toMap((arr->arr[0].toString()), (arr->Float.valueOf(arr[1].toString()))));
}

如你所见 topTenMoviesByGenreLikeIgnoreCaseMap 退货 Map 片名是关键。制造 Movie 实体您应该使用 JPQL 查询而不是本机查询。

72qzrwbm

72qzrwbm2#

试试这个:

@Query(value ="SELECT m.title as title, r.avg(rating) as rates " +
    "FROM movies m " +
    "INNER JOIN " +
    "ratings r " +
    "ON m.movieid = r.movieid " +
    "WHERE m.genre = LIKE %:genre% " +
    "GROUP BY r.avg(rating) " +
    "ORDER BY r.avg(rating) DESC " +
    "LIMIT 10",
    nativeQuery = true)

List<Movies> topTenMoviesByGenreLikeIgnoreCase(@Param("genre") String genre);    

default Map<Movies, Set<Ratings>> topTenMoviesByGenreLikeIgnoreCaseMap(@Param("genre") String genre) {
    return topTenMoviesByGenreLikeIgnoreCase(genre).stream().collect(
                                                            Collectors.toMap(
                                                            tuple -> (tuple.getTitle(),
                                                            tuple -> (tuple.getRates())
                                                         ));
}

相关问题