休眠:如何用另一张表的平均值填充一列?

时间:2019-01-17 19:43:04

标签: hibernate spring-data-jpa h2

我有两个实体,一个是单位,另一个是评论。 单位列之一是分数,即来自该特定单位评论的平均分数。如何用评论表中的平均分数填充列?

@Entity
@Table(name = "UNIT")
public class Unit {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "unit_id")
private Long id;

@Column(name = "unit_image")
private String image;

@Column(name = "unit_title")
private String title;

@Column(name = "unit_region")
private String region;

@Column(name = "unit_description")
private String description;

@Column(name = "unit_cancellation")
private String cancellation;

@Column(name = "unit_price")
private double price;

@Column(name = "unit_score")
private double score;

@OneToMany(mappedBy = "unit", cascade = {CascadeType.REMOVE,CascadeType.MERGE, CascadeType.REFRESH}, orphanRemoval = true, targetEntity = Review.class)
private List<Review> reviews;



@Entity
@Table(name = "REVIEW")
public class Review {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "review_id")
private Long id;

@Column(name = "review_score")
private double score;

@Column(name = "review_comment")
private String comment;

@ManyToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name = "unit_id", referencedColumnName = "unit_id", nullable = false)
private Unit unit;

我正在使用jpa / hibernate和h2数据库。

0 个答案:

没有答案