public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id")
private Integer employeeId;
@Column(name = "doj")
@Temporal(TemporalType.DATE)
private Date doj;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "id.employee")
private Set<EmployeeProject> employeeProject;
//
Project.java
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "project_id")
private Integer projectId;
@Column(name = "exp_end_date")
@Temporal(TemporalType.DATE)
private Date expEndDate;
@Column(name = "project_name")
private String projectName;
@Column(name = "start_date")
@Temporal(TemporalType.DATE)
private Date startDate;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "id.project")
private Set<EmployeeProject> employeeProject;
EmployeeProject.java
@IdClass(EmployeeProjectPK.class)
public class EmployeeProject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Id
@Column(name = "PROJECT_ID", insertable = false, updatable = false)
private int projectId;
@Id
@Column(name = "employee_id", insertable = false, updatable = false)
private int employeeId;
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
EmployeeProjectPK.Java
public class EmployeeProjectPK implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer projectId;
private Integer employeeId;
基于上面的实体设计,当我试图坚持一个'员工'时,我得到了以下异常。任何想法?
引起:java.lang.ClassCastException:org.hibernate.mapping.SimpleValue无法强制转换为org.hibernate.mapping.Component
答案 0 :(得分:2)
我认为您在Employee实体中映射的问题
你可以尝试使用
@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee")
private Set<EmployeeProject> employeeProject;
而不是
@OneToMany(cascade = CascadeType.ALL, mappedBy = "id.employee")
private Set<EmployeeProject> employeeProject;
也尝试在Project实体中更改它并使用
@OneToMany(cascade = CascadeType.ALL, mappedBy = "project")
private Set<EmployeeProject> employeeProject;
而不是
@OneToMany(cascade = CascadeType.ALL, mappedBy = "id.project")
private Set<EmployeeProject> employeeProject;
所以最后你应该替换
mappedBy =&#34; id.project&#34; 由 mappedBy =&#34;项目&#34;
和
mappedBy =&#34; id.employee&#34; mappedBy =&#34;员工&#34;