I have 2 entity classes. One employee should has one department. But one department can has many employees.
When I send to postman request insert employee, I want to trigger Department table.
@Table
@Entity(name = "employee")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Employee extends Auditable<String> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "employee_name", nullable = false)
private String employeeName;
@Column(name = "employee_surname", nullable = false)
private String employeeSurname;
@Column(name = "employee_email",
nullable = false,
unique = true)
private String employeeEmail;
@Column(name = "employee_identity_number",
nullable = false,
unique = true,
length = 11)
private String employeeIdentityNumber;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
Above code my Employee class.
@Table
@Entity(name = "department")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
@Column(name = "department_name", unique = true)
private String departmentName;
@OneToMany(mappedBy = "department")
private Set<Employee> employees;
}
Above code my Department class.
When I send postman request as JSON like following,
{
"employeeName": "Josh",
"employeeSurname": "Long",
"employeeEmail": "[email protected]",
"employeeIdentityNumber": "12345678912",
"department": 1
}
I know, department property is wrong. I should define as department object. But i want to send just department id.
And when I send this request, I want to trigger employees field in department table.
2
Answers
You could have a dto object like this
A controller mthod like this:
to my knowlage, this should do the job.
you can try this,
and for department,