You didn't provide
Foo.setBarList() implementation but I believe that you just have a one like
Code:
public void setBarList(List<Bar> barList) {
this.barList = barList;
}
I.e.
'Bar.foo' property is uninitialized.
The proper way to implement the method is below:
Code:
public void setBarList(List<Bar> barList) {
this.barList = barList;
for (Bar bar : barList) {
bar.setFoo(this);
}
}
Also you should specify one end of your bidirectional association as inverse and add not-null constraint. I.e. the mappings look as follows:
Code:
@Entity(name = "foo")
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
@JoinColumn(name = "foo_id", nullable = false)
@OrderBy("id")
private List<Bar> barList;
public void setBarList(List<Bar> barList) {
this.barList = barList;
for (Bar bar : barList) {
bar.setFoo(this);
}
}
public List<Bar> getBarList() {
return barList;
}
}
Code:
@Entity(name = "bar")
public class Bar {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(targetEntity = Foo.class)
@JoinColumn(name = "foo_id")
private Foo foo;
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
}