簡體   English   中英

Spring-子對象上沒有setParent的JPA OneToMany

[英]Spring - JPA OneToMany without setParent on Child object

我有一個父母表應該指向一些孩子,這是我的代碼:
家長

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

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<Child> childs;


    public List<Child> getChilds()
    {
        return this.childs;
    }

    public void setChilds(List<Child> childs)
    {
        this.childs = childs;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public Integer getId()
    {
        return this.id;
    }
}

小孩

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

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Parent parent;

    public void setParent(Parent parent)
    {
        this.parent = parent;
    }

    public Parent getParent()
    {
        return this.parent;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public Integer getId()
    {
        return this.id;
    }
}

子表應將parent_id存儲為外鍵,但在不設置child.setParent(parent)此列始終為null。
我不想調用child.setParent(parent)因為它會導致性能問題,因為我應該創建迭代項。

Child是關系的所有者,因此您必須調用child.setParent(parent)才能正確設置關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM