-1

https://www.baeldung.com/jpa-joincolumn-vs-mappedby

followed the same approach for creating oneToMany & ManyToOne relation mapping.

Even though used the @JoinColumn in owning side entity, as per "baeldung" example in the "email entity" but it keeps the foreign key as null.

When I used the @JoinColumn in parent entity i.e. as per in above example in "Employee" entity, then it worked.

but what's the reason behind this behavior of relation mapping ? why this example approach is incorrect ?

Here is my code repo link : https://github.com/TamtePrathamesh/sb_mapping between 'User' & 'mobile' while keeping oneToMany/ManyToOne mapping the mobile table not maintaining the foreign key the entry comes as "NULL". Mobile Mapping Table

1 Answers1

2

When dealing with a bidirectional association you need to update the association on both sides:

Mobile mobile = ...
User user = ...
user.mobiles.add(mobile);
mobile.user = user;

save(user);
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30