我有两个实体,一个是单位,另一个是评论。 单位列之一是分数,即来自该特定单位评论的平均分数。如何用评论表中的平均分数填充列?
@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数据库。