如何使用带有复合键的@IdClass 注释
How to use @IdClass annotation with composite key
有人可以分享一个示例,两个实体在 EclipseLink (JPA 2.1) 的主从模式上使用带@IdClass 的复合键
这是来自 documentation
的示例
public class EmployeePK implements Serializable {
private long empId;
private long department;
public EmployeePK() {
}
public long getEmpId() {
return this.empId;
}
public void setEmpId(long empId) {
this.empId = empId;
}
public long getDepartment() {
return this.department;
}
public void setDepartment(long department) {
this.department = department;
}
public int hashCode() {
return (int)this.empId.hashCode();
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof EmployeePK)) return false;
EmployeePK pk = (EmployeePK) obj;
return pk.empId.equals(this.empId) && pk.department.equals(this.department);
}
}
和
@IdClass(EmployeePK.class)
@Entity
public class Employee implements Serializable{
@Id
long empId;
@Id
@ManyToOne
Department department;
...
}
有人可以分享一个示例,两个实体在 EclipseLink (JPA 2.1) 的主从模式上使用带@IdClass 的复合键
这是来自 documentation
的示例 public class EmployeePK implements Serializable {
private long empId;
private long department;
public EmployeePK() {
}
public long getEmpId() {
return this.empId;
}
public void setEmpId(long empId) {
this.empId = empId;
}
public long getDepartment() {
return this.department;
}
public void setDepartment(long department) {
this.department = department;
}
public int hashCode() {
return (int)this.empId.hashCode();
}
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof EmployeePK)) return false;
EmployeePK pk = (EmployeePK) obj;
return pk.empId.equals(this.empId) && pk.department.equals(this.department);
}
}
和
@IdClass(EmployeePK.class)
@Entity
public class Employee implements Serializable{
@Id
long empId;
@Id
@ManyToOne
Department department;
...
}