Hibernate联合主键

使用Hibernate配置联合主键的两种方式
1.主键字段和非主键字段在同一个实体类中,Hibernate配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<!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.krmsoft.ais.project.vo.ProVersion" table="PRO_VERSION">
        <comment>脱机版版本号</comment>
         <composite-id>
            <key-property name="verNo" type="java.lang.Integer">
                <column name="VER_NO" not-null="true">               
                <comment>版本号</comment>
                </column>
            </key-property>
            <key-property name="buildNo" type="java.lang.Integer">
                <column name="BUILD_NO" not-null="true">               
                <comment>升级号</comment>
                </column>
            </key-property>
        </composite-id>
        <property name="buildTime" type="java.lang.String">
            <column name="BUILD_TIME" length="32" not-null="true">
                <comment>升级日期</comment>
            </column>
        </property>
        <property name="verName" type="java.lang.String">
            <column name="VER_NAME">
                <comment>版本名称</comment>
            </column>
        </property>
        <property name="verDesc" type="java.lang.String">
            <column name="VER_DESC">
                <comment>版本描述</comment>
            </column>
        </property>
        <property name="creatorId" type="java.lang.String">
            <column name="CREATOR_ID" length="10" not-null="true">
                <comment>创建人</comment>
            </column>
        </property>
        <property name="createTime" type="java.lang.String">
            <column name="CREATE_TIME" length="20" not-null="true">
                <comment>创建时间</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

2.主键字段和非主键字段不在同一个实体类中,Hibernate配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<!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.krmsoft.ais.project.vo.ProVersion" table="PRO_VERSION">
        <comment>脱机版版本号</comment>
         <composite-id  name="id" class="com.krmsoft.ais.project.vo.ProVersionId">
            <key-property name="verNo" type="java.lang.Integer">
                <column name="VER_NO" not-null="true">               
                <comment>版本号</comment>
                </column>
            </key-property>
            <key-property name="buildNo" type="java.lang.Integer">
                <column name="BUILD_NO" not-null="true">               
                <comment>升级号</comment>
                </column>
            </key-property>
        </composite-id>
        <property name="buildTime" type="java.lang.String">
            <column name="BUILD_TIME" length="32" not-null="true">
                <comment>升级日期</comment>
            </column>
        </property>
        <property name="verName" type="java.lang.String">
            <column name="VER_NAME">
                <comment>版本名称</comment>
            </column>
        </property>
        <property name="verDesc" type="java.lang.String">
            <column name="VER_DESC">
                <comment>版本描述</comment>
            </column>
        </property>
        <property name="creatorId" type="java.lang.String">
            <column name="CREATOR_ID" length="10" not-null="true">
                <comment>创建人</comment>
            </column>
        </property>
        <property name="createTime" type="java.lang.String">
            <column name="CREATE_TIME" length="20" not-null="true">
                <comment>创建时间</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

你可能感兴趣的:(Hibernate,复合主键,composite-id,联合主键)