JPA not saving foreign key to @OneToMany relation

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

    In reply to Cletus' answer. I would say that it's important to have the @column annotation on the id fields, as well as all the sequence stuff. An alternative to using the mappedBy parameter of the @OneToMany annotation is to use the @JoinColumn annotation.

    As a kinda aside your implementation of addPhone needs looking at. It should probably be something like.

    public void addPhone(PhoneNumber phone) {
        if (phone == null) {
            return;
        } else {
            if (phoneNumbers == null) {
                phoneNumbers = new ArrayList();
            }
            phoneNumbers.add(phone);
            phone.setContact(this);
        }
    }
    

提交回复
热议问题