使用Spring将数据插入到彼此引用的表中

时间:2017-06-13 14:59:42

标签: java sql spring

我正在将数据导入到使用@Entity注释自动创建的表中。它适用于父表,但是当我将数据导入子表时,数据不会相互引用。

父表ProductPopularity

@Entity

public class ProductPopularity{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private long sales;

private long clicks;

@OneToMany(cascade = CascadeType.ALL)
@JsonManagedReference
private List<UserReview> reviews;

@NotNull
private long productID;

子表:

@Entity
public class UserReview {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private int quality;

private int connectivity;

@NotNull
long userID;

@ManyToOne(cascade = CascadeType.ALL)
@NotNull
@JsonBackReference
private ProductPopularity productPopularity;

我是import.sql文件:

INSERT INTO PRODUCT_POPULARITY (id, sales, clicks, productID) VALUES (1, 50, 150, 101);
INSERT INTO PRODUCT_POPULARITY (id, sales, clicks, productID) VALUES (2, 88, 250, 102);

INSERT INTO USER_REVIEW (id, quality, connectivity, userID, PRODUCT_POPULARITY_ID) VALUES(1, 5, 5, 100, 1);

在我的restController中,我通过pathVariable ProductID(review.getProductPopularity.getProductID())搜索ProductPopularity和UserReviews。

其余的电话... / 101 / productPopularity返回预期的 { “ID”:1, “销售”:50, “点击”:150, “的productID”:101}

然而,其余的调用... / 101 / userReviews返回am EmptyList []。通过Java代码手动插入用户评论时,它可以正常工作......

0 个答案:

没有答案