0
我是JPA的新手当尝试使用辅助表和组合键时遇到问题。带有组合键和辅助表的JPA
我收到以下错误消息,当我尝试添加,删除或更新:
提供了错误的类型预期ID:类EmployeePK,得到了类 java.lang.Integer中
@Entity
@IdClass(EmployeePK.class)
@Table(name="specialemployee")
@SecondaryTable(name = "employeeTypeAndSalary", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "employee_Id"),
@PrimaryKeyJoinColumn(name = "employee_Email") })
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
public enum EmployeeType {
WORKER, FOREMAN, MANAGEMENT
}
@Id
private int id;
@Column(name = "EMP")
@Embedded
private Name name;
@Id
private String email;
private Date birthDate;
@Lob
private String comments;
@Column(name = "EMP_SALARY", table = "employeeTypeAndSalary")
private double salary;
@Column(name = "EMP_TYPE", table = "employeeTypeAndSalary")
@Enumerated(EnumType.STRING)
private EmployeeType employeeType;
public Employee() {
super();
}
public Employee(int id, Name name, String email, double salary, String birthDate,
String comments, EmployeeType employeeType) {
super();
this.id = id;
this.name = name;
this.email = email;
this.salary = salary;
try {
this.birthDate = java.sql.Date.valueOf(birthDate);
} catch (Exception e) {
logging.error("error on creating date" + " :" + e);
this.birthDate = java.sql.Date.valueOf("1900-00-00");
}
this.comments = comments;
this.employeeType = employeeType;
}
//getters and setters
}
public class EmployeePK implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String email;
// non-arg default constructor
public EmployeePK() {
super();
}
public EmployeePK(int id, String email){
this.id = id;
this.email = email;
}
public int getId() {
return id;
}
protected void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
protected void setEmail(String email) {
this.email = email;
}
public boolean equals(Object o) {
return ((o instanceof EmployeePK) &&
email.equals(((EmployeePK)o).getEmail()) &&
id == ((EmployeePK) o).getId());
}
public int hashCode() {
return (int) (email.hashCode() + id);
}
}
@Embeddable
public class Name implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
public Name() {
super();
}
@Override
public String toString() {
return "Name [firstName=" + firstName + ", lastName=" + lastName + "]";
}
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
// getters and setters
}
我一直在看它一段时间,我不明白我做错了什么。任何的建议都受欢迎。
谢谢。
编辑:
Name name1 = new Name("Johnn", "Doe");
Employee employee1 = new Employee(1, name1, "[email protected]",
1857.87, "1976-05-12", "ready for promotion",
EmployeeType.MANAGEMENT);
addEmployee(employee1);
private static void addEmployee(Employee employee) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPA_excercise");
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
} catch (Exception e) {
logging.error("This error has occured when adding a employee"
+ " :" + e);
} finally {
em.close();
emf.close();
}
}
您尝试创建员工的代码在哪里?鉴于您的问题中的代码,您在Employee类('id'和'email')上定义了两个标识符,它们都不是'EmployeePK'。 – Perception
上面编辑的代码 –
包含完整的异常和堆栈 – James