Hibernate version: 3.1 beta1 (Annotations 3.1 beta4)
I have a very simple problem and am quite sure I'm doing something wrong here. I have the following case (leaving out some unnecessary code):
Code:
@Entity
class A {
@OneToMany(mappedBy="a", cascade = {CascadeType.ALL})
public Set<B> getBs() {...};
}
@Entity
class B {
@ManyToOne(optional=false, fetch=FetchType.EAGER)
public A getA() {...};
}
In other words, a bi-directional one-to-many relation that cascades all operations.
Now I attempt to do the following:
Code:
A a = new A();
a.addB(new B());
session.save(a);
All very simple. But this fails. Due to the CascadeType.ALL, an attempt is made to save the B object related to A. However, since there is no A yet, if B is saved, the foreign key constraint on A can not be satisfied. This constraint exists because of the optional=false attribute on the getA() method.
So for saving this, I first need to save the B object, then add it to A and then save A.
Is it impossible to do it the way I want, or am I missing something?