hibernate 联合主键 composite-id

<composite-id name="propertyName" class="ClassName" mapped="true|false" access="field|property|ClassName" node="element-name|." > <key-property name="propertyName" type="typename" column="column_name"/> <key-many-to-one name="propertyName class="ClassName" column="column_name"/> ...... </composite-id>

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

<composite-id> <key-property name="medicareNumber"/> <key-property name="dependent"/> </composite-id>

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

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

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

<composite-id class="MedicareId" mapped="true"> <key-property name="medicareNumber"/> <key-property name="dependent"/> </composite-id>

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

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

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

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

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

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

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

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

例子:

POJO

package com.pojo.basic; // Generated 2011-11-18 10:24:33 by Hibernate Tools 3.4.0.CR1 import java.math.BigDecimal; /** * CurId 幣別 */ public class CurId implements java.io.Serializable { private static final long serialVersionUID = 1L; private CurIdId id; //此处为联合主键 private String name; private BigDecimal excRto; private String accNoIr; private String accNoIp; private String accNoChk; private BigDecimal excRtoE; private BigDecimal excRtoI; private BigDecimal excRtoO; private String idSgt; private byte[] upDd; public CurId() { } public CurId(CurIdId id) { this.id = id; } public CurId(CurIdId id, String name, BigDecimal excRto, String accNoIr, String accNoIp, String accNoChk, BigDecimal excRtoE, BigDecimal excRtoI, BigDecimal excRtoO, String idSgt, byte[] upDd) { this.id = id; this.name = name; this.excRto = excRto; this.accNoIr = accNoIr; this.accNoIp = accNoIp; this.accNoChk = accNoChk; this.excRtoE = excRtoE; this.excRtoI = excRtoI; this.excRtoO = excRtoO; this.idSgt = idSgt; this.upDd = upDd; } public CurIdId getId() { return this.id; } public void setId(CurIdId id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public BigDecimal getExcRto() { return this.excRto; } public void setExcRto(BigDecimal excRto) { this.excRto = excRto; } public String getAccNoIr() { return this.accNoIr; } public void setAccNoIr(String accNoIr) { this.accNoIr = accNoIr; } public String getAccNoIp() { return this.accNoIp; } public void setAccNoIp(String accNoIp) { this.accNoIp = accNoIp; } public String getAccNoChk() { return this.accNoChk; } public void setAccNoChk(String accNoChk) { this.accNoChk = accNoChk; } public BigDecimal getExcRtoE() { return this.excRtoE; } public void setExcRtoE(BigDecimal excRtoE) { this.excRtoE = excRtoE; } public BigDecimal getExcRtoI() { return this.excRtoI; } public void setExcRtoI(BigDecimal excRtoI) { this.excRtoI = excRtoI; } public BigDecimal getExcRtoO() { return this.excRtoO; } public void setExcRtoO(BigDecimal excRtoO) { this.excRtoO = excRtoO; } public String getIdSgt() { return this.idSgt; } public void setIdSgt(String idSgt) { this.idSgt = idSgt; } public byte[] getUpDd() { return this.upDd; } public void setUpDd(byte[] upDd) { this.upDd = upDd; } } //联合主键bean

package com.pojo.basic; // Generated 2011-11-18 10:24:33 by Hibernate Tools 3.4.0.CR1 import java.util.Date; /** * CurIdId generated by hbm2java */ public class CurIdId implements java.io.Serializable { private static final long serialVersionUID = 1L; private String curId; private Date ijDd; public CurIdId() { } public CurIdId(String curId, Date ijDd) { this.curId = curId; this.ijDd = ijDd; } public String getCurId() { return this.curId; } public void setCurId(String curId) { this.curId = curId; } public Date getIjDd() { return this.ijDd; } public void setIjDd(Date ijDd) { this.ijDd = ijDd; } //重写equals 和hashcode方法 public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof CurIdId)) return false; CurIdId castOther = (CurIdId) other; return ((this.getCurId() == castOther.getCurId()) || (this.getCurId() != null && castOther.getCurId() != null && this.getCurId().equals( castOther.getCurId()))) && ((this.getIjDd() == castOther.getIjDd()) || (this.getIjDd() != null && castOther.getIjDd() != null && this.getIjDd() .equals(castOther.getIjDd()))); } public int hashCode() { int result = 17; result = 37 * result + (getCurId() == null ? 0 : this.getCurId().hashCode()); result = 37 * result + (getIjDd() == null ? 0 : this.getIjDd().hashCode()); return result; } 配置映射文件 hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2011-11-18 10:24:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.pojo.basic.CurId" table="CUR_ID" schema="dbo" catalog="DB_GTC">
        <composite-id name="id" class="com.pojo.basic.CurIdId">
            <key-property name="curId" type="string">
                <column name="CUR_ID" length="4" />
            </key-property>
            <key-property name="ijDd" type="timestamp">
                <column name="IJ_DD" length="23" />
            </key-property>
        </composite-id>
        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property>
        <property name="excRto" type="big_decimal">
            <column name="EXC_RTO" precision="28" scale="8" />
        </property>
        <property name="accNoIr" type="string">
            <column name="ACC_NO_IR" length="20" />
        </property>
        <property name="accNoIp" type="string">
            <column name="ACC_NO_IP" length="20" />
        </property>
        <property name="accNoChk" type="string">
            <column name="ACC_NO_CHK" length="20" />
        </property>
        <property name="excRtoE" type="big_decimal">
            <column name="EXC_RTO_E" precision="28" scale="8" />
        </property>
        <property name="excRtoI" type="big_decimal">
            <column name="EXC_RTO_I" precision="28" scale="8" />
        </property>
        <property name="excRtoO" type="big_decimal">
            <column name="EXC_RTO_O" precision="28" scale="8" />
        </property>
        <property name="idSgt" type="string">
            <column name="ID_SGT" length="10" />
        </property>
        <property name="upDd" type="binary">
            <column name="UP_DD" />
        </property>
    </class>
</hibernate-mapping>


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值