私は 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, "employee1@hotmail.com",
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();
}
}