我有这样的问题,我想从哪里提取电影标题和评级
@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*/
}
2条答案
按热度按时间vxqlmq5t1#
你可以用
Object[]
以此方式作为查询结果那么呢
如你所见
topTenMoviesByGenreLikeIgnoreCaseMap
退货Map
片名是关键。制造Movie
实体您应该使用JPQL
查询而不是本机查询。72qzrwbm2#
试试这个: