Java实体类设置联合主键_java - 如何在JPA中创建和处理复合主键

关键课程:

@Embeddable

@Access (AccessType.FIELD)

public class EntryKey implements Serializable {

public EntryKey() {

}

public EntryKey(final Long id, final Long version) {

this.id = id;

this.version = version;

}

public Long getId() {

return this.id;

}

public void setId(Long id) {

this.id = id;

}

public Long getVersion() {

return this.version;

}

public void setVersion(Long version) {

this.version = version;

}

public boolean equals(Object other) {

if (this == other)

return true;

if (!(other instanceof EntryKey))

return false;

EntryKey castOther = (EntryKey) other;

return id.equals(castOther.id) && version.equals(castOther.version);

}

public int hashCode() {

final int prime = 31;

int hash = 17;

hash = hash * prime + this.id.hashCode();

hash = hash * prime + this.version.hashCode();

return hash;

}

@Column (name = "ID")

private Long id;

@Column (name = "VERSION")

private Long operatorId;

}

实体类:

@Entity

@Table (name = "YOUR_TABLE_NAME")

public class Entry implements Serializable {

@EmbeddedId

public EntryKey getKey() {

return this.key;

}

public void setKey(EntryKey id) {

this.id = id;

}

...

private EntryKey key;

...

}

如何与其他版本复制?

您可以分离从提供程序检索的实体,更改Entry的键,然后将其作为新实体保留。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值