I have the following entity:
@Entity
@Data
@IdClass(ProjectEmployeeId.class)
public class ProjectEmployee {
@Id
@ManyToOne
private Project project;
@Id
@ManyToOne
private Employee employee;
@ManyToOne
private ProjectEmployeeRole projectEmployeeRole;
}
The id class which defines the composite primary key for the above entity:
@Data
public class ProjectEmployeeId implements Serializable {
private Project project;
private Employee employee;
}
Thats the repository:
public interface ProjectEmployeeRepository
extends CrudRepository<ProjectEmployee, ProjectEmployeeId> {
}
I tried to save a projectEmployee:
projectEmployeeRepository.save(projectEmployee);
which gives me the following error:
Resolved [org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'de.employee.Employee' for property 'employee'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'de.employee.Employee' for property 'employee': no matching editors or conversion strategy found]
Im using a composite primary key for the first time, so i´m not sure how it is supposed to work. The problem probably comes from defining CrudRepository<ProjectEmployee, ProjectEmployeeId>
. The second argument is supposed to represent the id. But ProjetEmployee
doesnt have one primary key, instead it has two so i thought i should add the id class into the id argument which does not work. How do i save an entity with a composite primary using IdClass key with a CrudRepository?