hibernate 联合主键 composite-id

  1. <composite-id  
  2.         name="propertyName"  
  3.         class="ClassName"  
  4.         mapped="true|false"  
  5.         access="field|property|ClassName"  
  6.         node="element-name|."  
  7.         >  
  8.   
  9.         <key-property name="propertyName" type="typename" column="column_name"/>  
  10.         <key-many-to-one name="propertyName class="ClassName" column="column_name"/>  
  11.         ......  
  12. </composite-id>  

如果表使用联合主键,你可以映射类的多个属性为标识符属性。 <composite-id>元素接受<key-property> 属性映射和<key-many-to-one>属性映射作为子元素。 

  1. <composite-id>  
  2.         <key-property name="medicareNumber"/>  
  3.         <key-property name="dependent"/>  
  4. </composite-id>  

你的持久化类必须重载equals()hashCode()方法,来实现组合的标识符的相等判断。 实现Serializable接口也是必须的。 

不幸的是,这种组合关键字的方法意味着一个持久化类是它自己的标识。除了对象自己之外, 没有什么方便的“把手”可用。你必须初始化持久化类的实例,填充它的标识符属性,再load() 组合关键字关联的持久状态。我们把这种方法称为embedded(嵌入式的组合标识符,在重要的应用中不鼓励使用这种用法。 

第二种方法我们称为mapped(映射式)组合标识符 (mapped composite identifier),<composite-id>元素中列出的标识属性不但在持久化类出现,还形成一个独立的标识符类。 

  1. <composite-id class="MedicareId" mapped="true">  
  2.         <key-property name="medicareNumber"/>  
  3.         <key-property name="dependent"/>  
  4. </composite-id>  

在这个例子中,组合标识符类MedicareId和实体类都含有medicareNumberdependent属性。标识符类必须重载equals()hashCode()并且实现Serializable接口。这种方法的缺点是出现了明显的代码重复。 

下面列出的属性是用来指定一个映射式组合标识符的:

  • mapped (可选, 默认为false): 指明使用一个映射式组合标识符,其包含的属性映射同时在实体类和组合标识符类中出现。

  • class (可选,但对映射式组合标识符必须指定): 作为组合标识符类使用的类名.  

  • name (可选,但对这种方法而言必须): 包含此组件标识符的组件类型的名字.

  • access (可选 - 默认为property): hibernate应该使用的访问此属性值的策略

  • class (可选 - 默认会用反射来自动判定属性类型 ): 用来作为组合标识符的组件类的类名

第三种方式,被称为identifier component(标识符组件)是我们对几乎所有应用都推荐使用的方式。 

例子:

POJO

  1. package com.pojo.basic;  
  2.   
  3. // Generated 2011-11-18 10:24:33 by Hibernate Tools 3.4.0.CR1  
  4.   
  5. import java.math.BigDecimal;  
  6.   
  7. /** 
  8.  * CurId 幣別 
  9.  */  
  10. public class CurId implements java.io.Serializable {  
  11.   
  12.     private static final long serialVersionUID = 1L;  
  13.     private CurIdId id;    //此处为联合主键  
  14.     private String name;  
  15.     private BigDecimal excRto;  
  16.     private String accNoIr;  
  17.     private String accNoIp;  
  18.     private String accNoChk;  
  19.     private BigDecimal excRtoE;  
  20.     private BigDecimal excRtoI;  
  21.     private BigDecimal excRtoO;  
  22.     private String idSgt;  
  23.     private byte[] upDd;  
  24.   
  25.     public CurId() {  
  26.     }  
  27.   
  28.     public CurId(CurIdId id) {  
  29.         this.id = id;  
  30.     }  
  31.   
  32.     public CurId(CurIdId id, String name, BigDecimal excRto, String accNoIr,  
  33.             String accNoIp, String accNoChk, BigDecimal excRtoE,  
  34.             BigDecimal excRtoI, BigDecimal excRtoO, String idSgt, byte[] upDd) {  
  35.         this.id = id;  
  36.         this.name = name;  
  37.         this.excRto = excRto;  
  38.         this.accNoIr = accNoIr;  
  39.         this.accNoIp = accNoIp;  
  40.         this.accNoChk = accNoChk;  
  41.         this.excRtoE = excRtoE;  
  42.         this.excRtoI = excRtoI;  
  43.         this.excRtoO = excRtoO;  
  44.         this.idSgt = idSgt;  
  45.         this.upDd = upDd;  
  46.     }  
  47.   
  48.     public CurIdId getId() {  
  49.         return this.id;  
  50.     }  
  51.   
  52.     public void setId(CurIdId id) {  
  53.         this.id = id;  
  54.     }  
  55.   
  56.     public String getName() {  
  57.         return this.name;  
  58.     }  
  59.   
  60.     public void setName(String name) {  
  61.         this.name = name;  
  62.     }  
  63.   
  64.     public BigDecimal getExcRto() {  
  65.         return this.excRto;  
  66.     }  
  67.   
  68.     public void setExcRto(BigDecimal excRto) {  
  69.         this.excRto = excRto;  
  70.     }  
  71.   
  72.     public String getAccNoIr() {  
  73.         return this.accNoIr;  
  74.     }  
  75.   
  76.     public void setAccNoIr(String accNoIr) {  
  77.         this.accNoIr = accNoIr;  
  78.     }  
  79.   
  80.     public String getAccNoIp() {  
  81.         return this.accNoIp;  
  82.     }  
  83.   
  84.     public void setAccNoIp(String accNoIp) {  
  85.         this.accNoIp = accNoIp;  
  86.     }  
  87.   
  88.     public String getAccNoChk() {  
  89.         return this.accNoChk;  
  90.     }  
  91.   
  92.     public void setAccNoChk(String accNoChk) {  
  93.         this.accNoChk = accNoChk;  
  94.     }  
  95.   
  96.     public BigDecimal getExcRtoE() {  
  97.         return this.excRtoE;  
  98.     }  
  99.   
  100.     public void setExcRtoE(BigDecimal excRtoE) {  
  101.         this.excRtoE = excRtoE;  
  102.     }  
  103.   
  104.     public BigDecimal getExcRtoI() {  
  105.         return this.excRtoI;  
  106.     }  
  107.   
  108.     public void setExcRtoI(BigDecimal excRtoI) {  
  109.         this.excRtoI = excRtoI;  
  110.     }  
  111.   
  112.     public BigDecimal getExcRtoO() {  
  113.         return this.excRtoO;  
  114.     }  
  115.   
  116.     public void setExcRtoO(BigDecimal excRtoO) {  
  117.         this.excRtoO = excRtoO;  
  118.     }  
  119.   
  120.     public String getIdSgt() {  
  121.         return this.idSgt;  
  122.     }  
  123.   
  124.     public void setIdSgt(String idSgt) {  
  125.         this.idSgt = idSgt;  
  126.     }  
  127.   
  128.     public byte[] getUpDd() {  
  129.         return this.upDd;  
  130.     }  
  131.   
  132.     public void setUpDd(byte[] upDd) {  
  133.         this.upDd = upDd;  
  134.     }  
  135.   
  136. }  
//联合主键bean
  1. package com.pojo.basic;  
  2.   
  3. // Generated 2011-11-18 10:24:33 by Hibernate Tools 3.4.0.CR1  
  4.   
  5. import java.util.Date;  
  6.   
  7. /** 
  8.  * CurIdId generated by hbm2java 
  9.  */  
  10. public class CurIdId implements java.io.Serializable {  
  11.   
  12.     private static final long serialVersionUID = 1L;  
  13.     private String curId;  
  14.     private Date ijDd;  
  15.   
  16.     public CurIdId() {  
  17.     }  
  18.   
  19.     public CurIdId(String curId, Date ijDd) {  
  20.         this.curId = curId;  
  21.         this.ijDd = ijDd;  
  22.     }  
  23.   
  24.     public String getCurId() {  
  25.         return this.curId;  
  26.     }  
  27.   
  28.     public void setCurId(String curId) {  
  29.         this.curId = curId;  
  30.     }  
  31.   
  32.     public Date getIjDd() {  
  33.         return this.ijDd;  
  34.     }  
  35.   
  36.     public void setIjDd(Date ijDd) {  
  37.         this.ijDd = ijDd;  
  38.     }  
  39.         //重写equals 和hashcode方法  
  40.     public boolean equals(Object other) {  
  41.         if ((this == other))  
  42.             return true;  
  43.         if ((other == null))  
  44.             return false;  
  45.         if (!(other instanceof CurIdId))  
  46.             return false;  
  47.         CurIdId castOther = (CurIdId) other;  
  48.   
  49.         return ((this.getCurId() == castOther.getCurId()) || (this.getCurId() != null  
  50.                 && castOther.getCurId() != null && this.getCurId().equals(  
  51.                 castOther.getCurId())))  
  52.                 && ((this.getIjDd() == castOther.getIjDd()) || (this.getIjDd() != null  
  53.                         && castOther.getIjDd() != null && this.getIjDd()  
  54.                         .equals(castOther.getIjDd())));  
  55.     }  
  56.   
  57.     public int hashCode() {  
  58.         int result = 17;  
  59.   
  60.         result = 37 * result  
  61.                 + (getCurId() == null ? 0 : this.getCurId().hashCode());  
  62.         result = 37 * result  
  63.                 + (getIjDd() == null ? 0 : this.getIjDd().hashCode());  
  64.         return result;  
  65.     }  
  1. <strong>配置映射文件 hbm.xml</strong>  
  1. <pre name="code" class="html"><?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!-- Generated 2011-11-18 10:24:34 by Hibernate Tools 3.4.0.CR1 -->  
  5. <hibernate-mapping>  
  6.     <class name="com.pojo.basic.CurId" table="CUR_ID" schema="dbo" catalog="DB_GTC">  
  7.         <composite-id name="id" class="com.pojo.basic.CurIdId">  
  8.             <key-property name="curId" type="string">  
  9.                 <column name="CUR_ID" length="4" />  
  10.             </key-property>  
  11.             <key-property name="ijDd" type="timestamp">  
  12.                 <column name="IJ_DD" length="23" />  
  13.             </key-property>  
  14.         </composite-id>  
  15.         <property name="name" type="string">  
  16.             <column name="NAME" length="20" />  
  17.         </property>  
  18.         <property name="excRto" type="big_decimal">  
  19.             <column name="EXC_RTO" precision="28" scale="8" />  
  20.         </property>  
  21.         <property name="accNoIr" type="string">  
  22.             <column name="ACC_NO_IR" length="20" />  
  23.         </property>  
  24.         <property name="accNoIp" type="string">  
  25.             <column name="ACC_NO_IP" length="20" />  
  26.         </property>  
  27.         <property name="accNoChk" type="string">  
  28.             <column name="ACC_NO_CHK" length="20" />  
  29.         </property>  
  30.         <property name="excRtoE" type="big_decimal">  
  31.             <column name="EXC_RTO_E" precision="28" scale="8" />  
  32.         </property>  
  33.         <property name="excRtoI" type="big_decimal">  
  34.             <column name="EXC_RTO_I" precision="28" scale="8" />  
  35.         </property>  
  36.         <property name="excRtoO" type="big_decimal">  
  37.             <column name="EXC_RTO_O" precision="28" scale="8" />  
  38.         </property>  
  39.         <property name="idSgt" type="string">  
  40.             <column name="ID_SGT" length="10" />  
  41.         </property>  
  42.         <property name="upDd" type="binary">  
  43.             <column name="UP_DD" />  
  44.         </property>  
  45.     </class>  
  46. </hibernate-mapping>  
  47. </pre><br>  
  48. <br>  
  49. <pre></pre>  
  50. <p></p>  
  51. <pre></pre>  
  52. <p></p> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值