具有复合键和辅助表的JPA

时间:2012-12-03 12:13:02

标签: java 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, "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();
    }
}

1 个答案:

答案 0 :(得分:0)

发现问题。添加方法没有任何问题。更新方法中的问题,我忘了更改日志文本,所以似乎问题是添加方法。问题已解决