Frits Walraven wrote:Yes, it is a bidirectional relationship because you have @OneToOne annotations on both sides of the relationship.
Seriously, this exact wording of yours is invaluable. On all the resources I've looked, no one has said simply that "annotations on both entities for bi-directional, only on parent for uni-directional"...
The cascade did not work because you have the cascade option (cascade = CascadeType.ALL) on the non-owner side of the relationship (that is where the mappedBy attribute is).
I see. If I had created my SQL tables with the FK on the Person entity, it would have worked. So essentially, with the FK on the FoodHabit table, the FoodHabit is the parent and Person the child entity.
Now, this means that the Person entity must have a FK for the Measurement entity, although in a Join table because it's a OneToMany relationship.
In other words there are two solutions to your problem:
set both sides of the relationship before you do the persist (that is what you did), ormove the mappedBy attribute to the FoodHabit Entity, and @JoinColumn to the Person Entity.
For the time being I'll stick to the bi-directional relationships in order to finish my project and not re-creating my DB tables, but changing to uni-directional is mandatory. To me it makes sense to go from Person to FoodHabit and not the other way around.
From the JPA specs:
3.2.4 Synchronization to the Database
Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change. In the case of unidirectional one-to-one and one-to-many relationships, it is the developer’s responsibility
Quite frankly I won't be making bi-directional relationships any more, now that you explained what's going on

I'll need to be extra careful with the Entities though, especially if I let the IDE auto-generate them. It doesn't mean the program is always right...