0

我是 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();
    }
}
4

1 回答 1

0

发现问题。添加方法没有任何问题。更新方法中的问题,我忘记更改日志记录文本,因此问题似乎出在 add 方法中。问题已解决

于 2012-12-05T07:01:02.757 回答