Every time I write a 'create' method in a DAO I have to construct the objects with the annotation @ManyToOne when they already exist and I only have the id. Like in the following example.
I have the class Employee:
@Entity
public class Employee
{
@Id
private Long id;
private String employeeName;
@ManyToOne
private Employer employer;
...
}
And in the DAO I have a method to create an employee using the id of an already existing Employer. This is how I usually solve it:
public class EmployeeDAO {
...
public void createEmployee(String name, String employerId) {
Employee employee = new Employee();
employee.setName(name);
Employer employer = new Employer();
employer.setId(employerId);
employee.setEmployer(employer);
save(employee);
}
...
}
I would like to know if there is a more elegant way of doing this without making the 'new Employer()'