在JPA中使用联合主键
发表于|更新于
|字数总计:306|阅读时长:1分钟|阅读量:
Spring Boot中使用JPA默认集成了Hibernater,当我们使用单主键的@Entity还是比较方便的,只需要加上@Id注解即可,那如果遇到两个列形成联合主键的场景,该如何定义@Entity呢?下文即为实践。
定义联合主键
import javax.persistence.Embeddable; import java.io.Serializable;
@Embeddable
public class VerifyResultPK implements Serializable{ private String cdaname; private Long cdaxh; public VerifyResultPK() { }
public VerifyResultPK(String cdaname, Long cdaxh) { this.cdaname = cdaname; this.cdaxh = cdaxh; } }
|
在Model上使用联合主键
import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; import java.io.Serializable; import java.sql.Timestamp;
@Entity @Table(name = "TAS_CDAYZ_YZJG") public class VerifyResultModel implements Serializable{ @EmbeddedId VerifyResultPK key; private Boolean yzzt; private Integer cws; private Long yzxh; private Timestamp yzsj; public VerifyResultModel(String cdaname, Long cdaxh, Boolean yzzt, Integer cws, Long yzxh, Timestamp yzsj) { VerifyResultPK key = new VerifyResultPK(cdaname,cdaxh); this.key = key; this.yzzt = yzzt; this.cws = cws; this.yzxh = yzxh; this.yzsj = yzsj; }
}
|
END