【发布时间】:2021-04-19 20:18:29
【问题描述】:
我有这样的查询,我想从哪里提取电影标题及其评分
@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个值的resultSet
在了解它之后,我发现例如此链接Spring Data: JPA repository findAll() to return *Map instead of List? 但我似乎不清楚它们的实现。
我不明白他们从第一个链接中从哪里获得 Long 值
default Map<Long, TransactionModel> findByClientIdMap(Long id) {
return findByClientId(id).stream().collect(Collectors.toMap(TransactionModel::getId, v -> v));
}
如何创建我的地图?
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*/
}
【问题讨论】:
标签: sql spring spring-boot jpa spring-data-jpa