This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

JPA JoinColumn vs mappedBy

What’s the difference

@Entity
public class Company {

    @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
    @JoinColumn(name = "companyIdRef", referencedColumnName = "companyId")
    privateList<Branch> branches; . }Copy the code
@Entity
public class Company {

    @OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY, mappedBy = "companyIdRef")
    privateList<Branch> branches; . }Copy the code

Answer a

MappedBy annotations should be used in the parent class (the company class) of a bidirectional relationship. In this case, it should be used in the Company class to refer to the member variable ‘Company’ of its subclass (Branch class).

The JoinColumn annotation is usually used to describe a mapping column (used to join an entity relationship). The annotation can be used in any class (parent and subclass), but it should only be used in one class (either parent or subclass). In this example, I’m using the subclass in a bidirectional relationship. To display a foreign key in the subclass (Branch class)

Here’s an example: parent class, Company

@Entity
public class Company {


    private int companyId;
    private String companyName;
    private List<Branch> branches;

    @Id
    @GeneratedValue
    @Column(name="COMPANY_ID")
    public int getCompanyId(a) {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }

    @Column(name="COMPANY_NAME")
    public String getCompanyName(a) {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    @OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="company")
    public List<Branch> getBranches(a) {
        return branches;
    }

    public void setBranches(List<Branch> branches) {
        this.branches = branches; }}Copy the code

Subclasses, Branch

@Entity
public class Branch {

    private int branchId;
    private String branchName;
    private Company company;

    @Id
    @GeneratedValue
    @Column(name="BRANCH_ID")
    public int getBranchId(a) {
        return branchId;
    }

    public void setBranchId(int branchId) {
        this.branchId = branchId;
    }

    @Column(name="BRANCH_NAME")
    public String getBranchName(a) {
        return branchName;
    }

    public void setBranchName(String branchName) {
        this.branchName = branchName;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="COMPANY_ID")
    public Company getCompany(a) {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company; }}Copy the code

Answer two

@joinColumn specifies that the entity is the owner of the relationship (corresponding to a table with a foreign key and its reference table). But the mappedBy property indicates that the entity on this side is in an inverse relationship, while the owner is in the other entity. This also means that you can use other tables in this class that you annotate with mappedBy.

In addition, the correct annotation for the code in question would look something like this:

@Entity
public class Company {
    @OneToMany(mappedBy = "company", orphanRemoval = true, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Branch> branches;
}

@Entity
public class Branch {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "companyId")
    private Company company;
}
Copy the code

The article translated from Stack Overflow:stackoverflow.com/questions/1…