JPA not saving foreign key to @OneToMany relation

前端 未结 6 1058
小蘑菇
小蘑菇 2020-12-25 13:50

I\'m using Spring with Hibernate as a JPA provider and are trying to get a @OneToMany (a contact having many phonenumbers) to save the foreign key in the phone numbers table

6条回答
  •  [愿得一人]
    2020-12-25 14:22

    If you want your relationship unidirectional i.e. can navigate from Contact to Phone's only, you need to add

    @JoinColumn(name = "contact_id", nullable = false)
    

    Under your @OneToMany on your parent entity.

    nullable = false IS VITAL if you want hibernate to populate the fk on the child table

提交回复
热议问题