[英]How to query reference table in Spring JPA
我有两个表 Item 和 Review Item class
@Entity
public class Item {
@Id
@GeneratedValue
private Long id;
@Column(length = 100)
@NotEmpty
private String title;
@Column(length = 200)
private String description;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
Set<Review> reviews = new HashSet<>();
public Item() {
}
public Item(String title, String description) {
this.title = title;
this.description = description;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public Set<Review> getReviews() {
return reviews;
}
public void addReview(Review review) {
reviews.add(review);
review.setItem(this);
}
@Override
public String toString() {
return "\nItem{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", reviews=" + reviews +
'}';
}
}
审查表
@Entity
public class Review {
@Id
@GeneratedValue
private Long id;
private Double rating;
@Length(max=200)
private String comment;
@ManyToOne(optional = false)
private Item item;
@ManyToOne(optional = false)
private User author;
public Review() {
}
public Review(Double rating, String comment, User author) {
this.rating = rating;
this.comment = comment;
this.author = author;
}
public Long getId() {
return id;
}
public Double getRating() {
return rating;
}
public String getComment() {
return comment;
}
public Item getItem() {
return item;
}
public User getAuthor() {
return author;
}
void setItem(Item item) {
this.item = item;
}
@Override
public String toString() {
return "\nReview{" +
"id=" + id +
", rating=" + rating +
", comment='" + comment + '\'' +
'}';
}
}
我想使用 Spring JPA 编写查询来查找平均评分低于例如 (6) 的项目。 单个项目将有多个评级,因此特定项目的平均评级应小于 6。我尝试了很多方法但无法计算。
public interface ItemRepository extends CrudRepository<Item, Long> {
@Query(
value = "SELECT i FROM Item i where (select AVG(rating) from Review where rating < :rating) > :rating",
nativeQuery = true)
List<Item> findItemsWithAverageRatingLowerThan(@Param("rating") Double rating);
}
请纠正我出错的地方。
我不明白这部分
特定项目的平均评分应小于 6
但简单地说,您将 JPQL 语法与本机 SQL 语法混合在一起。 当您使用函数或特定的 SQL 运算符时,本机版本仅为 go:
SELECT * FROM ITEM i JOIN REVIEW r ON i.id = r.item_id where AVG(r.rating) < :rating
nativeQuery = true
Item
和Review
表的连接查询示例:
@Repository
public interface ItemRepository extends CrudRepository<Item, Long> {
@Query(value = "SELECT i FROM Item i where (select AVG(r.rating) FROM Review r where r.item = i) < :rating")
List<Item> findItemsWithAverageRatingLowerThan(@Param("rating") Double rating);
}
测试数据:
insert into item(id, description, title) values(1, 'description', 'title1');
insert into item(id, description, title) values(2, 'description', 'title2');
insert into review(id, rating, comment, item_id) values(1, 3, 'comment', 1);
insert into review(id, rating, comment, item_id) values(2, 3, 'comment', 1);
insert into review(id, rating, comment, item_id) values(3, 3, 'comment', 1);
insert into review(id, rating, comment, item_id) values(4, 30, 'comment', 2);
insert into review(id, rating, comment, item_id) values(5, 3, 'comment', 2);
insert into review(id, rating, comment, item_id) values(6, 15, 'comment', 2);
输入值: 6
Output: id = 1 的项目实体
查询返回 AVG Review 评分小于 6 的项目
Hibernate 生成本机查询:
select
item0_.id as id1_1_,
item0_.description as descript2_1_,
item0_.title as title3_1_
from
item item0_
where
(
select
avg(review1_.rating)
from
review review1_
where
review1_.item_id=item0_.id
)<?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.