Netbeans 11 - JavaEE 8 - WildFly 14 - Spring Boot 2.x
Netbeans 11 - JavaEE 8 - WildFly 14 - Spring Boot 2.x
In the backing bean where I construct the objects to be persisted, after I set the FoodHabit to the Person, I must also set the Person to the FoodHabit. Seems like I did a bi-directional relationship after all?
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
Frits Walraven wrote:Yes, it is a bidirectional relationship because you have @OneToOne annotations on both sides of the relationship.
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).
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), or move the mappedBy attribute to the FoodHabit Entity, and @JoinColumn to the Person Entity.
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...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
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"...
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.
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...
public class Person implements Serializable {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "personId")
private FoodHabit foodHabit;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "personId")
private List<Measurement> measurementList;
}
public class Measurement implements Serializable {
@JoinColumn(name = "person_id", referencedColumnName = "person_id")
@ManyToOne(optional = false)
private Person personId;
}
public class FoodHabit implements Serializable {
@JoinColumn(name = "person_id", referencedColumnName = "person_id")
@OneToOne(optional = false)
private Person personId;
}
Netbeans 11 - JavaEE 8 - WildFly 14 - Spring Boot 2.x