联合主键的问题

注解联合主键实体类的主要步骤

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      ;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值