我有一个父表,应该指向一些Childs,这是我的代码:
家长:
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)
,则此列始终为空
我不想调用child.setParent(parent)
,因为它会导致性能问题,因为我应该创建迭代。
答案 0 :(得分:0)
Child
是该关系的所有者,因此您必须致电child.setParent(parent)
以正确设置该关系。