简体   繁体   中英

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.
I don't want to call child.setParent(parent) because it will cause performance issue because i should create iterate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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