这次有个需求,在数据库建表时发现,主键为复合主键:
这里采用@IdClass注解的方式来实现复合主键;
思路
编写一个复合主键类UserProjectMultiKeysClass;
通过@IdClass注释在实体中标注复合主键;
可以通过EntityManager获取数据,或者是直接在Repository 里写方法;
package com.hiocs.cron.entity;
import java.io.Serializable;
/**
* @ Author :johnbarrowman65
* @ Date :Created in 16:32 2019/4/8
* @ Description:EnteredInfo 联合主键
*/
public class UserProjectMultiKeysClass implements Serializable {
private static final long serialVersionUID = 1L;
private String bmbbm;//
private String xh;//
private String xxdm;//
public UserProjectMultiKeysClass() {
}
public UserProjectMultiKeysClass(String bmbbm, String xh, String xxdm) {
this.bmbbm = bmbbm;
this.xh = xh;
this.xxdm = xxdm;
}
public String getBmbbm() {
return bmbbm;
}
public void setBmbbm(String bmbbm) {
this.bmbbm = bmbbm;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXxdm() {
return xxdm;
}
public void setXxdm(String xxdm) {
this.xxdm = xxdm;
}
// ***重写hashCode与equals方法***
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((bmbbm == null) ? 0 : bmbbm.hashCode());
result = PRIME * result + ((xh == null) ? 0 : xh.hashCode());
result = PRIME * result + ((xxdm == null) ? 0 : xxdm.hashCode());
return result;
}
@Override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(getClass() != obj.getClass()){
return false;
}
final UserProjectMultiKeysClass other = (UserProjectMultiKeysClass)obj;
if(bmbbm == null){
if(other.bmbbm != null){
return false;
}
}else if(!bmbbm.equals(other.bmbbm)){
return false;
}
if(xh == null){
if(other.xh != null){
return false;
}
}else if(!xh.equals(other.xh)){
return false;
}
if(xxdm == null){
if(other.xxdm != null){
return false;
}
}else if(!xxdm.equals(other.xxdm)){
return false;
}
return true;
}
}
注意:
复合主键类必须满足:
1. 实现Serializable接口;
2. 有默认的public无参数的构造方法;
3. 重写equals和hashCode方法。equals方法用于判断两个对象是否相同,EntityManger通过find方法来查找Entity时,是根据equals的返回值来判断的。hashCode方法返回当前对象的哈希码;
实体类
package com.hiocs.cron.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* 报名信息实体类
* @author
*/
@Entity
@Table(name = "XX_ENTERED_INFO")
@IdClass(UserProjectMultiKeysClass.class)
public class EnteredInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String bmbbm;//
private String xh;//
private String gbzydm;//
private String batch_number;
private String xxdm;//
private String xm;//
private int xb_id;//
private int mzdm_id;//
private int zzmm_id;//
private String jg;//
private int zjlxId; //
private String spzh;//
private String spdz;//
private String dwmc;
private String dwdz;
private String dwsszj;
private String education;
private String lxdz_city;
private String lxdz_town;
private String lxdz;//
private String lxrxm;//
private String lxrgx;//
private String lxrdh;//
private String mobile;
private Boolean sfcyjnzs;
private int ly_id;//
private Long bmsj;//
private int state_id;//
private String updated;
private String memo;//
private int sfebss;//
private int sfsfzhf;//
private String sfzf;//
@Override
public String toString() {
return "EnteredInfo{" +
"bmbbm='" + bmbbm + '\'' +
", xh='" + xh + '\'' +
", gbzydm='" + gbzydm + '\'' +
", batch_number='" + batch_number + '\'' +
", xxdm='" + xxdm + '\'' +
", xm='" + xm + '\'' +
", xb_id=" + xb_id +
", mzdm_id=" + mzdm_id +
", zzmm_id=" + zzmm_id +
", jg='" + jg + '\'' +
", zjlxId=" + zjlxId +
", spzh='" + spzh + '\'' +
", spdz='" + spdz + '\'' +
", dwmc='" + dwmc + '\'' +
", dwdz='" + dwdz + '\'' +
", dwsszj='" + dwsszj + '\'' +
", education='" + education + '\'' +
", lxdz_city='" + lxdz_city + '\'' +
", lxdz_town='" + lxdz_town + '\'' +
", lxdz='" + lxdz + '\'' +
", lxrxm='" + lxrxm + '\'' +
", lxrgx='" + lxrgx + '\'' +
", lxrdh='" + lxrdh + '\'' +
", mobile='" + mobile + '\'' +
", sfcyjnzs=" + sfcyjnzs +
", ly_id=" + ly_id +
", bmsj=" + bmsj +
", state_id=" + state_id +
", updated='" + updated + '\'' +
", memo='" + memo + '\'' +
", sfebss=" + sfebss +
", sfsfzhf=" + sfsfzhf +
", sfzf='" + sfzf + '\'' +
'}';
}
public String getUpdated() {
return updated;
}
public void setUpdated(String updated) {
this.updated = updated;
}
public String getSfzf() {
return sfzf;
}
public void setSfzf(String sfzf) {
this.sfzf = sfzf;
}
public int getSfebss() {
return sfebss;
}
public void setSfebss(int sfebss) {
this.sfebss = sfebss;
}
public int getSfsfzhf() {
return sfsfzhf;
}
public void setSfsfzhf(int sfsfzhf) {
this.sfsfzhf = sfsfzhf;
}
@Id
public String getBmbbm() {
return bmbbm;
}
public void setBmbbm(String bmbbm) {
this.bmbbm = bmbbm;
}
@Id
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getGbzydm() {
return gbzydm;
}
public void setGbzydm(String gbzydm) {
this.gbzydm = gbzydm;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getBatch_number() {
return batch_number;
}
public void setBatch_number(String batch_number) {
this.batch_number = batch_number;
}
@Id
public String getXxdm() {
return xxdm;
}
public void setXxdm(String xxdm) {
this.xxdm = xxdm;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public int getXb_id() {
return xb_id;
}
public void setXb_id(int xb_id) {
this.xb_id = xb_id;
}
public int getMzdm_id() {
return mzdm_id;
}
public void setMzdm_id(int mzdm_id) {
this.mzdm_id = mzdm_id;
}
public int getZzmm_id() {
return zzmm_id;
}
public void setZzmm_id(int zzmm_id) {
this.zzmm_id = zzmm_id;
}
public String getJg() {
return jg;
}
public void setJg(String jg) {
this.jg = jg;
}
public int getZjlxId() {
return zjlxId;
}
public void setZjlxId(int zjlxId) {
this.zjlxId = zjlxId;
}
public String getSpzh() {
return spzh;
}
public void setSpzh(String spzh) {
this.spzh = spzh;
}
public String getSpdz() {
return spdz;
}
public void setSpdz(String spdz) {
this.spdz = spdz;
}
public String getDwmc() {
return dwmc;
}
public void setDwmc(String dwmc) {
this.dwmc = dwmc;
}
public String getDwdz() {
return dwdz;
}
public void setDwdz(String dwdz) {
this.dwdz = dwdz;
}
public String getDwsszj() {
return dwsszj;
}
public void setDwsszj(String dwsszj) {
this.dwsszj = dwsszj;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getLxdz_city() {
return lxdz_city;
}
public void setLxdz_city(String lxdz_city) {
this.lxdz_city = lxdz_city;
}
public String getLxdz_town() {
return lxdz_town;
}
public void setLxdz_town(String lxdz_town) {
this.lxdz_town = lxdz_town;
}
public String getLxdz() {
return lxdz;
}
public void setLxdz(String lxdz) {
this.lxdz = lxdz;
}
public String getLxrxm() {
return lxrxm;
}
public void setLxrxm(String lxrxm) {
this.lxrxm = lxrxm;
}
public String getLxrgx() {
return lxrgx;
}
public void setLxrgx(String lxrgx) {
this.lxrgx = lxrgx;
}
public String getLxrdh() {
return lxrdh;
}
public void setLxrdh(String lxrdh) {
this.lxrdh = lxrdh;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Boolean getSfcyjnzs() {
return sfcyjnzs;
}
public void setSfcyjnzs(Boolean sfcyjnzs) {
this.sfcyjnzs = sfcyjnzs;
}
public int getLy_id() {
return ly_id;
}
public void setLy_id(int ly_id) {
this.ly_id = ly_id;
}
public Long getBmsj() {
return bmsj;
}
public void setBmsj(Long bmsj) {
this.bmsj = bmsj;
}
public int getState_id() {
return state_id;
}
public void setState_id(int state_id) {
this.state_id = state_id;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
注意:
1. @IdClass标注用于标注实体所使用主键规则的类;
2. 在实体中同时标注主键的属性,比如这段代码中的userId
,projectId
以及timestamp
;