覆盖hibernate子类中的主键

时间:2016-07-26 08:27:11

标签: java hibernate composite-primary-key hbmxml

我有一个AbstractAccount hbm文件,我将accountNumber设置为primarykey。我正在Account.hbm.xml文件中扩展此课程,我希望将此主键作为countryCodeaccountNumber composite key。我怎么能这样做?

Abstract.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">

<hibernate-mapping>
    <class name="com.*.*.*.AbstractAccount" 
    abstract="true" 
    table="ACCOUNT" 
    lazy="false">
        <id name="accountNumber" type="java.lang.String">
            <column name="ACCOUNT_NUMBER" length="16" />
            <generator class="assigned" />
        </id>
       <discriminator column="ACCOUNT_TYPE" type="string"/>
       ................
    </class>
</hibernate-mapping>

Account.hbm.xml

    

    <property name="modifiedDate" type="java.util.Date">
        <column name="MODIFIED_DATE"/>
    </property>
    <property name="countryCode" type="java.lang.String">
        <column name="COUNTRY_CODE"/>            
    </property>
    ...............
</subclass>   

我尝试过如下添加复合键,但它无效。

<composite-id name="account_country">
<key-property name="countryCode" column="COUNTRY_CODE" />
<key-property name="accountNumber" column="ACCOUNT_NUMBER" />
</composite-id>

1 个答案:

答案 0 :(得分:0)

Abstract.hbm.xml

<hibernate-mapping>  
    /* Abstract class properites */

     <joined-subclass name="Account" table="tablename">

    /* Account class properites */

     </join-subclass>
</hibernate-mapping>