如果你有一个像
public class EmployeePK implements Serializable {
private String empName;
private Date birthDay;
public EmployeePK() {
}
public String getName() {
return this.empName;
}
public void setName(String name) {
this.empName = name;
}
public Date getBirthDay() {
return this.birthDay;
}
public void setBirthDay(Date date) {
this.birthDay = date;
}
public int hashCode() {
return (int)this.empName.hashCode();
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof EmployeePK)) return false;
EmployeePK pk = (EmployeePK) obj;
return pk.birthDay.equals(this.birthDay) && pk.empName.equals(this.empName);
}
}
和
@IdClass(EmployeePK.class)
@Entity
public class Employee implements Serializable{
@Id String empName;
@Id Date birthDay;
...
public Employee (String empName, Date birthDay){
this.empName= empName;
this.birthDay= birthDay;
}
...
}
你如何进行更新查询?
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPA_Compositekey");
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Employee anemployee = em.find( **WHAT DO YOU FILL HERE **)
...
或者我是否必须使用 PK 类中的对象以及如果您只有一个需要更新的人该怎么办。
谢谢大家