rupal82 wrote:
...
now i want to save parent n child record at a time
I am doing
parent.addChild(<childlist>)
and it raise this error..I think parent is not persisted while saving child
or may be you guys can point me in right direction
Why do you use
@JoinColumn at all? You can use just a
@OneToMany and
@ManyToOne mappings. Another note is that you didn't define inverted association end:
Code:
@Entity
public class Parent {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY, mappedBy = "parent")
private List<Child> childs;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Child> getChilds() {
return childs;
}
public void setChilds(List<Child> childs) {
this.childs = childs;
for (Child child : childs) {
child.setParent(this);
}
}
}
Code:
@Entity
public class Child {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Parent parent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}