JPA not saving foreign key to @OneToMany relation

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

    You have to manage the Java relationships yourself. For this kind of thing you need something like:

    @Entity
    public class Contact {
      @Id
      private Long id;
    
      @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "contact")
      private List<Phone> phoneNumbers;
    
      public void addPhone(PhoneNumber phone) {
         if (phone != null) {
            if (phoneNumbers == null) {
                phoneNumbers = new ArrayList<Phone>();          
            }
            phoneNumbers.add(phone);
            phone.setContact(this);
         }
      }
    
      ...
    }
    
    @Entity
    public class Phone {
      @Id
      private Long id;
    
      @ManyToOne
      private Contact contact;
    
      ...
    }
    
    0 讨论(0)
  • 2020-12-25 14:18

    If the Contact-Phone relationship is unidirectional, you can also replace mappedBy in @OneToMany annotation with @JoinColumn(name = "contact_id").

    @Entity
    public class Contact {
      @Id
      private Long id;
    
      @OneToMany(cascade = CascadeType.PERSIST)
      @JoinColumn(name = "contact_id")
      private List<Phone> phoneNumbers;
    
      // normal getter/setter
      ...
    }
    
    @Entity
    public class PhoneNumber {
      @Id
      private Long id;
    
      ...
    }
    

    Similar in JPA @OneToMany -> Parent - Child Reference (Foreign Key)

    0 讨论(0)
  • 2020-12-25 14:21

    I don't think the addPhone method is necessary, you only have to set the contact in the phone object:

    phone.setContact(contact);
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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<Phone>();
            }
            phoneNumbers.add(phone);
            phone.setContact(this);
        }
    }
    
    0 讨论(0)
  • 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<Phone> phones;
    }
    
    @Entity
    public class Phone {
        @Id
        private Long id;
        private Long contactId;
    }
    
    0 讨论(0)
提交回复
热议问题