2012-12-03 23 views
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(); 
    } 
} 
+1

您嘗試創建員工的代碼在哪裏?鑑於您的問題中的代碼,您在Employee類('id'和'email')上定義了兩個標識符,它們都不是'EmployeePK'。 – Perception

+0

上面編輯的代碼 –

+0

包含完整的異常和堆棧 – James

回答

0

發現問題。添加方法沒有任何問題。問題在更新方法,我忘記改變日誌文本,所以它似乎是問題在添加方法。問題解決