0

Given this image, I want to know if I should use JoinColumn at the same time I use mappedBy in other-file. Or, if I do not need to use mappedBy, then it's ok!

Can someone tell me why do we need to use mappedBy?

Employee.java

@Id
@GeneratedValue
@Column(name="employee_id")
private Long employeeId;

@Column(name="firstname")
private String firstname;

@Column(name="lastname")
private String lastname;

@Column(name="birth_date")
private Date birthDate;

@Column(name="cell_phone")
private String cellphone;

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

public Employee() {

}

public Employee(String firstname, String lastname, String phone) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.birthDate = new Date(System.currentTimeMillis());
    this.cellphone = phone;
}

// Getter and Setter methods

Department.java

@Id
@GeneratedValue
@Column(name="DEPARTMENT_ID")
private Long departmentId;

@Column(name="DEPT_NAME")
private String departmentName;

@OneToMany(mappedBy="department")
private Set<employee> employees;

// Getter and Setter methods

felipe.zkn
  • 2,012
  • 7
  • 31
  • 63
user2298562
  • 21
  • 1
  • 6

1 Answers1

2

Why do you need to use "mappedBy" ? You use it for defining that a relation is BIDIRECTIONAL and specifying what is the other side of the relation (for 1-1 you specify it at the side with the FK, and for 1-N you specify it at the Collection side). So you DON'T "HAVE TO" specify it if your relation is UNIDIRECTIONAL. Reading any half decent JPA docs would tell you that; aren't the docs for your implementation telling you this?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29