注解联合主键实体类的主要步骤
1 为符合主键,建立一个复合主键类,这个类包括两个字段,(有几个字段组成主键 就包含几个字段 )这个复合主键类实现Serializable接口,有public 无参的构造方法 重写equals 和hashcode方法,代码如下:
public class Demo_pk implements Serializable {
private long id ;
private String goods_sn ;
public Demo_pk() {
super();
}
public long getId() {
return id;
}
public String getGoods_sn() {
return goods_sn;
}
public void setId(long id) {
this.id = id;
}
public void setGoods_sn(String goods_sn) {
this.goods_sn = goods_sn;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((goods_sn == null) ? 0 : goods_sn.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cux_e3_order_line_pk other = (Cux_e3_order_line_pk) obj;
if (goods_sn == null) {
if (other.goods_sn != null)
return false;
} else if (!goods_sn.equals(other.goods_sn))
return false;
if (id != other.id)
return false;
return true;
}
2.在实体类里面用idclass标示复合主键类 (set、get 及tostring 没写)
@Entity
@Table(name = "DEMO")
@IdClass(Demo_pk.class)
public class Demo implements Serializable {
@Id
private long id ;
private String order_sn ;
private String deal_code ;
@Id
private String goods_sn ;
}