欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA复合主键使用

程序员文章站 2022-04-24 22:31:59
...

原文链接:https://blog.csdn.net/mypop/article/details/6149538
数据库表
create table RENT_CERT_VENDOR_TYPE
(
CERT_TYPE_ID NUMBER not null,
VENDOR_ID NUMBER not null,
CERT_TYPE_VENDOR_ID NUMBER not null
)
;
alter table RENT_CERT_VENDOR_TYPE
add constraint PK_RENT_CERT_VENDOR_TYPE primary key (CERT_TYPE_ID, VENDOR_ID);
alter table RENT_CERT_VENDOR_TYPE
add constraint FK_CERT_VENDOR_CERT_TYPE_ID foreign key (CERT_TYPE_ID)
references RENT_CERT_TYPE (ID);
alter table RENT_CERT_VENDOR_TYPE
add constraint FK_CERT_VENDOR_VENDOR_ID foreign key (VENDOR_ID)
references RENT_VENDOR (ID);

一、使用@EmbeddedId

eclipse生成的复合主键类
@Embeddable
public class RentCertVendorTypePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@Column(name="CERT_TYPE_ID")
private long certTypeId;

@Column(name="VENDOR_ID")
private long vendorId;

public RentCertVendorTypePK() {
}
public long getCertTypeId() {
	return this.certTypeId;
}
public void setCertTypeId(long certTypeId) {
	this.certTypeId = certTypeId;
}
public long getVendorId() {
	return this.vendorId;
}
public void setVendorId(long vendorId) {
	this.vendorId = vendorId;
}

public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!(other instanceof RentCertVendorTypePK)) {
		return false;
	}
	RentCertVendorTypePK castOther = (RentCertVendorTypePK)other;
	return 
		(this.certTypeId == castOther.certTypeId)
		&& (this.vendorId == castOther.vendorId);

}

public int hashCode() {
	final int prime = 31;
	int hash = 17;
	hash = hash * prime + ((int) (this.certTypeId ^ (this.certTypeId >>> 32)));
	hash = hash * prime + ((int) (this.vendorId ^ (this.vendorId >>> 32)));
	
	return hash;
}

}

主键类必需满足下列条件:
(1)必需被序列化
(2)必需有一个公共的无参构造方法
(3)必需实现equals()和hashCode()方法

使用主键类
@Entity
@Table(name=“RENT_CERT_VENDOR_TYPE”)
public class RentCertVendorType implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private RentCertVendorTypePK id;

@Column(name="CERT_TYPE_VENDOR_ID")
private BigDecimal certTypeVendorId;

//bi-directional many-to-one association to RentCertType
@ManyToOne
@JoinColumn(name="CERT_TYPE_ID")
private RentCertType rentCertType;

public RentCertVendorType() {
}

public RentCertVendorTypePK getId() {
	return this.id;
}

public void setId(RentCertVendorTypePK id) {
	this.id = id;
}

public BigDecimal getCertTypeVendorId() {
	return this.certTypeVendorId;
}

public void setCertTypeVendorId(BigDecimal certTypeVendorId) {
	this.certTypeVendorId = certTypeVendorId;
}

public RentCertType getRentCertType() {
	return this.rentCertType;
}

public void setRentCertType(RentCertType rentCertType) {
	this.rentCertType = rentCertType;
}

}

使用eclipse生成JPA实体的向导界面中,复合主键类的key generator指定为auto,则插件会自动识别数据库表中的复合主键,并生成复合主键类。

这种方法在Entity中获取复合主键中的某列属性如VENDOR_ID时,需要rentCertVendorType.getRentCertVendorTypePK().getVendorId(),其中rentCertVendorType为RentCertVendorType类对象。

二、使用@IdClass

主键类
public class RentCertVendorTypePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

private long certTypeId;

private long vendorId;

public RentCertVendorTypePK() {
}
public long getCertTypeId() {
	return this.certTypeId;
}
public void setCertTypeId(long certTypeId) {
	this.certTypeId = certTypeId;
}
public long getVendorId() {
	return this.vendorId;
}
public void setVendorId(long vendorId) {
	this.vendorId = vendorId;
}

public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!(other instanceof RentCertVendorTypePK)) {
		return false;
	}
	RentCertVendorTypePK castOther = (RentCertVendorTypePK)other;
	return 
		(this.certTypeId == castOther.certTypeId)
		&& (this.vendorId == castOther.vendorId);

}

public int hashCode() {
	final int prime = 31;
	int hash = 17;
	hash = hash * prime + ((int) (this.certTypeId ^ (this.certTypeId >>> 32)));
	hash = hash * prime + ((int) (this.vendorId ^ (this.vendorId >>> 32)));
	
	return hash;
}

}

Entity
@IdClass(RentCertVendorTypePK.class)
@Entity
@Table(name=“RENT_CERT_VENDOR_TYPE”)
public class RentCertVendorType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name=“CERT_TYPE_ID”)
private long certTypeId;

@Id
    @Column(name="VENDOR_ID")
private long vendorId;

@Column(name="CERT_TYPE_VENDOR_ID")
private BigDecimal certTypeVendorId;

//bi-directional many-to-one association to RentCertType
@ManyToOne
@JoinColumn(name="CERT_TYPE_ID")
private RentCertType rentCertType;

public RentCertVendorType() {
}

public RentCertVendorTypePK getId() {
	return this.id;
}

public void setId(RentCertVendorTypePK id) {
	this.id = id;
}

public BigDecimal getCertTypeVendorId() {
	return this.certTypeVendorId;
}

public void setCertTypeVendorId(BigDecimal certTypeVendorId) {
	this.certTypeVendorId = certTypeVendorId;
}

public RentCertType getRentCertType() {
	return this.rentCertType;
}

public void setRentCertType(RentCertType rentCertType) {
	this.rentCertType = rentCertType;
}

}

这种方法在Entity中获取复合主键中的某列属性如VENDOR_ID时,只需要rentCertVendorType.getVendorId(),其中rentCertVendorType为RentCertVendorType类对象。