联合主键三种实现方式

联合主键三种实现方式
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

联合主键可以通过Hibernate注解 进行映射,下面为大家展示三种实现方式:

一、方法一(本人喜欢使用这种方式,使用主键类字段时可以当做正常字段一样使用)

1.将联合主键的字段单独放在一个类中,该类需要重写equals和hashcode方法。

2.在主类中(该类包含联合主键类中的字段)将联合主键字段都注解为@Id。

3.最后在该类上加上注解:@IdClass(联合主键类.class),用来关联主键类

代码示例:

复制代码

 1 /**
 2  * 
 3  */
 4 package kklazy.acqinstmanagement.model;
 5 import javax.persistence.Column;
 6 import kklazy.persistence.model.SupportModel;
 7 
 8 /**
 9  * @author10  *  主键类
11  */
12 public class RouteProfitPK extends SupportModel{
13     /**
14      * 
15      */
16     private static final long serialVersionUID = -4243768063352460725L;
17     /*复合主键值*/
18     private String orgcode;//机构号
19     private String transType;//交易类型(前端)
20     
21     /**
22      * 无参数的public构造方法,必须要有
23      */
24     public RouteProfitPK() {
25         
26     }
27     /**
28      * @return the orgcode
29      */
30     @Column(name="ORGCODE")
31     public String getOrgcode() {
32         return orgcode;
33     }
34     /**
35      * @param orgcode the orgcode to set
36      */
37     public void setOrgcode(String orgcode) {
38         this.orgcode = orgcode;
39     }
40     
41     /**
42      * @return the transType
43      */
44     @Column(name="TRANSTYPE")
45     public String getTransType() {
46         return transType;
47     }
48     /**
49      * @param transType the transType to set
50      */
51     public void setTransType(String transType) {
52         this.transType = transType;
53     }
54     /* (non-Javadoc)
55      * @see java.lang.Object#hashCode()
56      */
57     @Override
58     public int hashCode() {
59         final int prime = 31;
60         int result = 1;
61         result = prime * result + ((orgcode == null) ? 0 : orgcode.hashCode());
62         result = prime * result + ((transType == null) ? 0 : transType.hashCode());
63         return result;
64     }
65     /* (non-Javadoc)
66      * @see java.lang.Object#equals(java.lang.Object)
67      */
68     @Override
69     public boolean equals(Object obj) {
70         if (this == obj)
71             return true;
72         if (obj == null)
73             return false;
74         if (getClass() != obj.getClass())
75             return false;
76         RouteProfitPK other = (RouteProfitPK) obj;
77         if (orgcode == null) {
78             if (other.orgcode != null)
79                 return false;
80         } else if (!orgcode.equals(other.orgcode))
81             return false;
82         if (transType == null) {
83             if (other.transType != null)
84                 return false;
85         } else if (!transType.equals(other.transType))
86             return false;
87         return true;
88     }
89     
90 
91 
92 }

复制代码

复制代码

  1 package kklazy.acqinstmanagement.model;
  2 import java.util.Date;
  3 import javax.persistence.Column;
  4 import javax.persistence.Entity;
  5 import javax.persistence.Id;
  6 import javax.persistence.IdClass;
  7 import javax.persistence.Table;
  8 import javax.persistence.Transient;
  9 import kklazy.persistence.model.SupportModel;
 10 
 11 /**
 12  * @author 
 13  * 主表
 14  */
 15 @Entity
 16 @Table(name="ROUTE_PROFIT")
 17 @IdClass(RouteProfitPK.class)
 18 public class RouteProfit extends SupportModel{
 19     private String orgcode;//机构号
 20     private String profitName;//分润名称
 21     private String transType;//交易类型(前端)
 22     private String payPeriod;//分润周期【00-日,01-周(1-7),02-月XX号(1-28)】格式为:00|    (日)01|4   (每周4)02|12  (每月12日)
 23     private String profitId;//分润方ID
 24     private String status;//状态(00_正常01_禁用)
 25     private Date entDate;//建立日期
 26     private String operaName;//操作员
 27     private String auditing;//审核(操作员
 28     private String memo;//备注
 29     
 30     private String transTypeName;//交易类型名称(页面显示)
 31     /**
 32      * @return the orgcode
 33      */
 34     @Column(name="ORGCODE")
 35     @Id
 36     public String getOrgcode() {
 37         return orgcode;
 38     }
 39     /**
 40      * @param orgcode the orgcode to set
 41      */
 42     public void setOrgcode(String orgcode) {
 43         this.orgcode = orgcode;
 44     }
 45     /**
 46      * @return the profitName
 47      */
 48     @Column(name="PROFIT_NAME")
 49     public String getProfitName() {
 50         return profitName;
 51     }
 52     /**
 53      * @param profitName the profitName to set
 54      */
 55     public void setProfitName(String profitName) {
 56         this.profitName = profitName;
 57     }
 58     /**
 59      * @return the transType
 60      */
 61     @Column(name="TRANSTYPE")
 62     @Id
 63     public String getTransType() {
 64         return transType;
 65     }
 66     /**
 67      * @param transType the transType to set
 68      */
 69     public void setTransType(String transType) {
 70         this.transType = transType;
 71     }
 72     /**
 73      * @return the payPeriod
 74      */
 75     @Column(name="PAYPERIOD")
 76     public String getPayPeriod() {
 77         return payPeriod;
 78     }
 79     /**
 80      * @param payPeriod the payPeriod to set
 81      */
 82     public void setPayPeriod(String payPeriod) {
 83         this.payPeriod = payPeriod;
 84     }
 85     /**
 86      * @return the profitId
 87      */
 88     @Column(name="PROFIT_ID")
 89     public String getProfitId() {
 90         return profitId;
 91     }
 92     /**
 93      * @param profitId the profitId to set
 94      */
 95     public void setProfitId(String profitId) {
 96         this.profitId = profitId;
 97     }
 98     /**
 99      * @return the status
100      */
101     @Column(name="STATUS")
102     public String getStatus() {
103         return status;
104     }
105     /**
106      * @param status the status to set
107      */
108     public void setStatus(String status) {
109         this.status = status;
110     }
111     /**
112      * @return the entDate
113      */
114     @Column(name="ENTDATE")
115     public Date getEntDate() {
116         return entDate;
117     }
118     /**
119      * @param entDate the entDate to set
120      */
121     public void setEntDate(Date entDate) {
122         this.entDate = entDate;
123     }
124     /**
125      * @return the operaName
126      */
127     @Column(name="OPERANAME")
128     public String getOperaName() {
129         return operaName;
130     }
131     /**
132      * @param operaName the operaName to set
133      */
134     public void setOperaName(String operaName) {
135         this.operaName = operaName;
136     }
137     /**
138      * @return the auditing
139      */
140     @Column(name="AUDITING")
141     public String getAuditing() {
142         return auditing;
143     }
144     /**
145      * @param auditing the auditing to set
146      */
147     public void setAuditing(String auditing) {
148         this.auditing = auditing;
149     }
150     /**
151      * @return the memo
152      */
153     @Column(name="MEMO")
154     public String getMemo() {
155         return memo;
156     }
157     /**
158      * @param memo the memo to set
159      */
160     public void setMemo(String memo) {
161         this.memo = memo;
162     }
163     /**
164      * @return the transTypeName
165      */
166     @Transient
167     public String getTransTypeName() {
168         return transTypeName;
169     }
170     /**
171      * @param transTypeName the transTypeName to set
172      */
173     public void setTransTypeName(String transTypeName) {
174         this.transTypeName = transTypeName;
175     }
176     
177     
178     
179 
180 }

复制代码

 

二、方法二

1.将联合主键的字段单独放在一个类中,该类需要重写equals和hascode方法。

2.在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用(新增主键类属性),并生成set和get方法。

3.最后为该类属性添加注解为@EmbeddedId

代码示例:

复制代码

package kklazy.acqinstmanagement.model;
import javax.persistence.Column;
import kklazy.persistence.model.SupportModel;

/**
 * @author 
 *  主键类
 */
public class RouteMerchFeePK extends SupportModel {

    /**
     * 
     */
    private static final long serialVersionUID = 1747760395347120749L;
     /*复合主键值*/
    private String feeId;        //手续费规则ID
    private String txnCode;      //交易类型
    private String fAmount;      //机构限额金额
    
    
    /**
     * 无参数的public构造方法,必须要有
     */
    public RouteMerchFeePK() {

    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((fAmount == null) ? 0 : fAmount.hashCode());
        result = prime * result + ((feeId == null) ? 0 : feeId.hashCode());
        result = prime * result + ((txnCode == null) ? 0 : txnCode.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RouteMerchFeePK other = (RouteMerchFeePK) obj;
        if (fAmount == null) {
            if (other.fAmount != null)
                return false;
        } else if (!fAmount.equals(other.fAmount))
            return false;
        if (feeId == null) {
            if (other.feeId != null)
                return false;
        } else if (!feeId.equals(other.feeId))
            return false;
        if (txnCode == null) {
            if (other.txnCode != null)
                return false;
        } else if (!txnCode.equals(other.txnCode))
            return false;
        return true;
    }

    

    

    /**
     * @return the feeId
     */
    @Column(name="FEE_ID")
    public String getFeeId() {
        return feeId;
    }

    /**
     * @param feeId the feeId to set
     */
    public void setFeeId(String feeId) {
        this.feeId = feeId;
    }

    /**
     * @return the txnCode
     */
    @Column(name="TXN_CODE")
    public String getTxnCode() {
        return txnCode;
    }

    /**
     * @param txnCode the txnCode to set
     */
    public void setTxnCode(String txnCode) {
        this.txnCode = txnCode;
    }

    /**
     * @return the fAmount
     */
    @Column(name="F_AMOUNT")
    public String getfAmount() {
        return fAmount;
    }

    /**
     * @param fAmount the fAmount to set
     */
    public void setfAmount(String fAmount) {
        this.fAmount = fAmount;
    }

}

复制代码

复制代码

  1 /**
  2  * 
  3  */
  4 package kklazy.acqinstmanagement.model;
  5 
  6 import java.math.BigDecimal;
  7 import java.util.Date;
  8 
  9 import javax.persistence.AttributeOverride;
 10 import javax.persistence.AttributeOverrides;
 11 import javax.persistence.Column;
 12 import javax.persistence.EmbeddedId;
 13 import javax.persistence.Entity;
 14 import javax.persistence.Table;
 15 import org.apache.commons.lang3.StringUtils;
 16 import kklazy.persistence.model.SupportModel;
 17 
 18 /**
 19  * @author 
 20  *  主表
 21  *
 22  */
 23 @Entity
 24 @Table(name="ROUTE_MERCH_FEE")
 25 public class RouteMerchFee extends SupportModel {
 26 
 27     public static final String FIXED_SEPARATOR = "|";
 28     public static final String FEE_TYPE_PERCENT = "01";
 29     /**
 30      * 
 31      */
 32     private static final long serialVersionUID = -6959012843279862852L;
 33     private RouteMerchFeePK id;//PK
 34     private String feeType;      //手续费类型
 35     private String debltFee;     //借记卡-手续费
 36     private String creditFee;    //贷记卡-手续费
 37     private String status;       //状态
 38     private Date endDate;      //建立日期
 39     private String operaName;      //操作员
 40     private Date updDate;      //修改日期
 41     private String auditing;     //审核
 42     private String memo;         //备注
 43     
 44     
 45     
 46 
 47     /**
 48      * @return the id
 49      */
 50      @EmbeddedId
 51     public RouteMerchFeePK getId() {
 52         return id;
 53     }
 54     /**
 55      * @param id the id to set
 56      */
 57     public void setId(RouteMerchFeePK id) {
 58         this.id = id;
 59     }
 60     
 61     /**
 62      * @return the feeType
 63      */
 64     @Column(name="FEETYPE")
 65     public String getFeeType() {
 66         return feeType;
 67     }
 68     /**
 69      * @param feeType the feeType to set
 70      */
 71     public void setFeeType(String feeType) {
 72         this.feeType = feeType;
 73     }
 74     /**
 75      * @return the debltFee
 76      */
 77     @Column(name="DEBIT_FEE")
 78     public String getDebltFee() {
 79         return debltFee;
 80     }
 81     /**
 82      * @param debltFee the debltFee to set
 83      */
 84     public void setDebltFee(String debltFee) {
 85         this.debltFee = debltFee;
 86     }
 87     
 88     public void setPageValueToDebltFee(){
 89         if(feeType.equals(FEE_TYPE_PERCENT)&& debitRate != null&& StringUtils.isNotEmpty(debitRate.trim()) ){
 90 //            this.debltFee=String.valueOf(Double.valueOf(debitRate)/100.00) ;
 91             this.debltFee = String.valueOf(BigDecimal.valueOf(Double.valueOf(debitRate).doubleValue()).divide(new BigDecimal("100")));
 92         }else{
 93             this.debltFee=debitRate;
 94         }
 95         this.debltFee=this.debltFee+"|"+this.debitRateMax+"|"+this.debitRateMin;
 96     }
 97     /**
 98      * @return the creditFee
 99      */
100     @Column(name="CREDIT_FEE")
101     public String getCreditFee() {
102         return creditFee;
103     }
104     /**
105      * @param creditFee the creditFee to set
106      */
107     public void setCreditFee(String creditFee) {
108         this.creditFee = creditFee;
109     }
110     
111     public void setPageValueToCreditFee(){
112         if(feeType.equals(FEE_TYPE_PERCENT) && crebitRate !=null && StringUtils.isNotEmpty(crebitRate.trim())){
113 //            this.creditFee=String.valueOf(Double.valueOf(crebitRate)/100.00);
114             this.creditFee= String.valueOf(BigDecimal.valueOf(Double.valueOf(crebitRate).doubleValue()).divide(new BigDecimal("100")));
115         }else{
116             this.creditFee=crebitRate;
117         }
118         this.creditFee=this.creditFee+"|"+this.crebitRateMax+"|"+this.crebitRateMin;
119     }
120     /**
121      * @return the status
122      */
123     @Column(name="STATUS")
124     public String getStatus() {
125         return status;
126     }
127     /**
128      * @param status the status to set
129      */
130     public void setStatus(String status) {
131         this.status = status;
132     }
133     /**
134      * @return the endDate
135      */
136     @Column(name="ENTDATE")
137     public Date getEndDate() {
138         return endDate;
139     }
140     /**
141      * @param endDate the endDate to set
142      */
143     public void setEndDate(Date endDate) {
144         this.endDate = endDate;
145     }
146     /**
147      * @return the operaName
148      */
149     @Column(name="OPERANAME")
150     public String getOperaName() {
151         return operaName;
152     }
153     /**
154      * @param operaName the operaName to set
155      */
156     public void setOperaName(String operaName) {
157         this.operaName = operaName;
158     }
159     /**
160      * @return the updDate
161      */
162     @Column(name="UPDDATE")
163     public Date getUpdDate() {
164         return updDate;
165     }
166     /**
167      * @param updDate the updDate to set
168      */
169     public void setUpdDate(Date updDate) {
170         this.updDate = updDate;
171     }
172     /**
173      * @return the auditing
174      */
175     @Column(name="AUDITING")
176     public String getAuditing() {
177         return auditing;
178     }
179     /**
180      * @param auditing the auditing to set
181      */
182     public void setAuditing(String auditing) {
183         this.auditing = auditing;
184     }
185     /**
186      * @return the memo
187      */
188     @Column(name="MEMO")
189     public String getMemo() {
190         return memo;
191     }
192     /**
193      * @param memo the memo to set
194      */
195     public void setMemo(String memo) {
196         this.memo = memo;
197     }
198 
199 
200 }

复制代码

 

三、方法三

1.将联合主键的字段单独放在一个类中,该类需要重写equals和hascode方法。

2.该主键类注解为@Embeddable。

3.最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用(新增主键类属性),并生成set和get方法,并将该引用注解为@Id。

代码示例:

复制代码

/**
 * 主键类
*/
@Embeddable  
public class TestPK{  
  
    private static final long serialVersionUID = -3304319243957837925L;  
    private long id ;  
    private String name ;  
    /** 
     * @return the id 
     */  
    public long getId() {  
        return id;  
    }  
    /** 
     * @param id the id to set 
     */  
    public void setId(long id) {  
        this.id = id;  
    }  
    /** 
     * @return the name 
     */  
    public String getName() {  
        return name;  
    }  
    /** 
     * @param name the name to set 
     */  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    @Override  
    public boolean equals(Object o) {  
        if(o instanceof Testkey0101){  
            Testkey01 key = (TestKey01)o ;  
            if(this.id == key.getId() && this.name.equals(key.getName())){  
                return true ;  
            }  
        }  
        return false ;  
    }  
      
    @Override  
    public int hashCode() {  
        return this.name.hashCode();  
    }  
      
}  

复制代码

复制代码

 1 /*
 2  * 主表类
 3 */
 4 @Entity  
 5 @Table(name="Test")  
 6 public class Test {  
 7   
 8     private static final long serialVersionUID = 3524215936351012384L;  
 9     private String address ;  
10     private int age ;  
11     private String email ;  
12     private String phone ;  
13     @Id
14     private TestKey01 testKey ;
15     ......
16 }

复制代码

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18402

发表评论