I have the following endpoint:
@PostMapping
Employee createEmployee(@Valid @RequestBody Employee newEmployee, BindingResult bindingResult) {
return employeeRepository.save(newEmployee);
}
I can send POST requests as JSON and have an employee created, but how do I link associated entities, such as a Department
entity?
When I send a post request, it looks like this (I know it's not valid JSON, it's just browser console output):
departmentId: 1
emailAddress: "dillon@james.com"
firstName: "Dillon"
lastName: "James"
phoneNumber: undefined
The department exists in my database, and has an ID of 1. I thought that Spring would automatically set the departmentId somehow on my Employee
model, but it doesn't seem to do that, or I must be missing something.
I'm basically trying to set the department of the newly created employee, but how I have it currently doesn't seem to do so.
For reference, this is how my Employee
entity is defined:
@Entity
@Getter
@Setter
public class Employee {
@Id
@GeneratedValue
private Long id;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
@Column(unique = true)
private String emailAddress;
@ManyToOne
private Department department;
private String phoneNumber;
}