JPA not saving foreign key to @OneToMany relation

前端 未结 6 1059
小蘑菇
小蘑菇 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:24

    Try this sample:

    @Entity
    public class Contact {
        @Id
        private Long id;
    
        @JoinColumn(name = "contactId")
        @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
        private Set phones;
    }
    
    @Entity
    public class Phone {
        @Id
        private Long id;
        private Long contactId;
    }
    

提交回复
热议问题