在多对一关系中插入/更新

时间:2020-01-14 08:11:18

标签: java hibernate jpa one-to-many many-to-one

1.Layout.java

@Entity
Public class Layout 
{
private int layoutId;    ---- primaary key autoincrement
private String layoutName;

@OneToMany( mappedBy = "layout" ,fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true )
private List<LayoutDetails> layoutdetails;
}

2.LayoutDetail.java

@Entity
public class LayoutDetail
{
private int layoutdetailid; ---- primaary key autoincrement

private String columnName;

@ManyToOne( fetch = FetchType.LAZY,cascade= {
            CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
    @JoinColumn(name = "layoutid")
private Layout layout;



}

仅在LayoutDetail中有什么方法可以插入和更新吗?

我尝试以常规方式执行此操作,但它说layoutid不能为null,插入失败

1 个答案:

答案 0 :(得分:0)

我尝试了您的示例,但效果很好。我可以在两个类中分别创建和更新。

我所做的唯一更改(基于您共享的代码)是对实体使用@Id@GeneratedValue注释。