I have two classes with a unidirectional relationship:
@Entity
public class A {
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="fk_column")
private List<B> bList;
...
}
@Entity
public class B {
...
}
When I do this first time:
A a = new A();
B b = new B();
a.getBList().add(b);
em.merge(a);
then a as well as b is persisted initial in the database and b has got the right foreign key to a.
But when I do this in another transaction:
A a = loadFromDatabaseByJPQLQuery();
B anotherB = new B();
a.getBList().add(anotherB);
em.merge(a);
then anotherB is persist indeed, but it's foreign key to a in fk_column is null.
How do I make it that the key is stored correctly?