I have been playing around with JPA and came across this scenario and wanted to know how to solve this.
I have 2 tables namely Company and Employee. So here an employee can work for only 1 company, therefore @OneToOne uni-directional mapping is done in Employee class. And the company details in the Company table would already exist.
So when I try to insert a record into Employee table, I even create a reference to the company, add it to the Employee class and save it using my EmployeeRepository's save method. Since the company_id would already exist in Company table, it should just refer it instead of creating a new company. But this is not happening.
I get an exception saying company object is still in the transient state.
I can solve this by adding CascadeType.All
but I don't need to insert the company rather I have to just link the company to the employee.
Here are the code snippets.
Employee Class
@Entity
@Table(name = "employee")
@Setter
@Getter
public class Employee
{
@Id
@GeneratedValue
private Long id;
@Column(name = "employee_id")
private Integer employeeId;
@Column(name = "employee_name")
private String employee_name;
@OneToOne
@JoinColumn(name = "company_id")
private Company company;
}
Company class
@Entity
@Table(name = "company")
@Setter
@Getter
public class Company
{
@Id
@GeneratedValue
@Column(name = "company_id")
private Long id;
@Column(name = "company_name")
private String companyName;
}
Company Table
company_id company_name
1 Dell
2 Apple
3 Microsoft
Here is how I am creating the Employee object
Employee emp = new Employee();
emp.setEmployeeId(10);
emp.setEmployeeName("Michael");
Company company = new Company();
company.setId(1);
emp.setCompany(company);
employeeRepository.save(emp);
Please, someone, let me know how to link the company class to an employee class rather than saving one more company.