使用@IdClass
下面是定义一个主键类:
public class xxPK implements Serializable {
private String n;
private Long s;
public xxPK () {
}
...
@Override
public int hashCode() {
...
return result;
}
@Override
public boolean equals(Object obj) {
...
return true;
}
}
主键类要满足一下要求:
必须可序列化
必须具备共有的无参构造函数
必须实现hashCode和equals方法
现在在xx类必须具有xxPK类所具有的成员属性,并且这些成员属性都要标注@Id注释
@IdClass(xxPK.class)
@Entity
public class xx{
@Id
private String n;
@Id
private Long s;
private String a;
...
}