What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association

后端 未结 8 1937
日久生厌
日久生厌 2020-11-22 02:35

What is the difference between:

@Entity
public class Company {

    @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
    @JoinColumn(name = \"c         


        
8条回答
  •  礼貌的吻别
    2020-11-22 02:43

    I'd just like to add that @JoinColumn does not always have to be related to the physical information location as this answer suggests. You can combine @JoinColumn with @OneToMany even if the parent table has no table data pointing to the child table.

    How to define unidirectional OneToMany relationship in JPA

    Unidirectional OneToMany, No Inverse ManyToOne, No Join Table

    It seems to only be available in JPA 2.x+ though. It's useful for situations where you want the child class to just contain the ID of the parent, not a full on reference.

提交回复
热议问题