Spring 持续失败,JSON 有 FK,FK 为空

Spring Persist Failure, JSON with FK, FK null

我有 2 个实体,PurchaseRequest (PR) 和 PurchaseRequestLineItem (PRLI)。他们遵循一种典型的关系模式,因为 PR 有很多 PRLI。我在 PRLI 上用@OneToMany 注释了 PR,在 PRLI 上用@ManyToOne 注释了 PRLI。我可以很好地插入 PR,但是如果我尝试自己插入 PRLI,并引用 PR,我会不断收到错误 "Column 'PurchaseRequestID' cannot be null"。下面是我的代码。我发现如果删除 PRLI purchaseRequest 变量中的 @JsonIgnore 注释,插入将起作用,但由于无限递归,getAll 会失败。我该如何解决?

JSON(是的,这些 ID 有效):

{
    "PurchaseRequest": {
        "Id":658
    },
    "ProductID": 2,
    "Quantity": 1
}

PurchaseRequest.java:

@Entity
@Table(name="purchaserequest")
public class PurchaseRequest implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @ManyToOne
    @JoinColumn(name="UserID") 
    private User user;
    ....
    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="purchaserequestid")
    private List<PurchaseRequestLineItem> lineItems;

PurchaseRequestLineItem.java

@Entity
@Table(name="purchaserequestlineitem")
public class PurchaseRequestLineItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @ManyToOne
    @JoinColumn(name="PurchaseRequestID")
    @JsonIgnore
    private PurchaseRequest purchaseRequest;
    private int productID;
    private int quantity;

不用@JsonIgnore 也可以用两个注解来解决无限递归问题: --@JsonManagedReference

--@JsonBackReference

@JsonManagedReference 私人列表 lineItems;

@JsonBackReference 私人购买请求购买请求;