简体   繁体   English

Spring-子对象上没有setParent的JPA OneToMany

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

I have a Parent table that should point to some Childs, Here are my codes: 我有一个父母表应该指向一些孩子,这是我的代码:
Parent : 家长

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;
    }
}

Child : 小孩

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;
    }
}

Child table should store parent_id as foreign key, but without setting child.setParent(parent) this column is always null. 子表应将parent_id存储为外键,但在不设置child.setParent(parent)此列始终为null。
I don't want to call child.setParent(parent) because it will cause performance issue because i should create iterate. 我不想调用child.setParent(parent)因为它会导致性能问题,因为我应该创建迭代项。

Child是关系的所有者,因此您必须调用child.setParent(parent)才能正确设置关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM