Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How @JoinColumn and @MappedBy works

I am confused over working on @JoinColumn and @MappedBy.

Consider following example Here is my Department class with unidirectional relationship

@Entity
@Table(name = "DEPARTMENT")
public class Department {
    @Id
    @Column(name = "DEPARTMENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer departmentId;
    @Column(name = "DEPARTMENT_NAME")
    private String departmentName;
    @Column(name = "LOCATION")
    private String location;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "DEPARTMENT_ID")
    private List<Employee> employees = new ArrayList<>();
}

I have a list of employees in Department class over which I have specified @JoinColumn so it will add department_id FK in employee table.

but with bidirectional relationship I will define the classes like

Department.java
@Entity
@Table(name = "DEPARTMENT")
public class Department {
    @Id
    @Column(name = "DEPARTMENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer departmentId;
    @Column(name = "DEPARTMENT_NAME")
    private String departmentName;
    @Column(name = "LOCATION")
    private String location;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "department")
    private List<Employee> employees = new ArrayList<>();
}

Employee.java
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
    @Id
    @SequenceGenerator(name = "emp_seq", sequenceName = "seq_employee")
    @GeneratedValue(generator = "emp_seq")
    @Column(name = "EMPLOYEE_ID")
    private Integer employeeId;
    @Column(name = "EMPLOYEE_NAME")
    private String employeeName;

    @ManyToOne
    @JoinColumn(name = "DEPARTMENT_ID")
    private Department department;
}

but with bidirectional relationship why do I need to place @JoinColumn over department in Employee class and write @MappedBy over list of employees in Department class?

update

so how does @MappedBy and @JoinColumn works?

like image 412
eatSleepCode Avatar asked Sep 07 '15 13:09

eatSleepCode


1 Answers

Actually, seems you're not using @MappedBy annotation but mappedBy = "department" in @OneToMany.

In simple words, mappedBy tells to hibernate that key of relationship is on the other table (class in this case).

Think on this: usually when you link 2 tables in any DB System, just 1 of those has the foreign key constraint to the other one right?

What MappedBy allows you to link from the table not containing the constraint to the other table.

About @JoinColumn it's easier, here you have in table foreign key, so you tell hibernate this is not only a column, but a column that must join a table.

The join column is declared with the @JoinColumn annotation which looks like the @Column annotation. It has one more parameters named referencedColumnName. This parameter declares the column in the targeted entity that will be used to the join. Note that when using referencedColumnName to a non primary key column, the associated class has to be Serializable. Also note that the referencedColumnName to a non primary key column has to be mapped to a property having a single column (other cases might not work).

Find HERE hibernate mapping documentations.

like image 72
Jordi Castilla Avatar answered Sep 21 '22 01:09

Jordi Castilla