0

我有两个父表和一个子表,下面提到了复合键:

Employee: 
        employeeId1 (PK)
        employeeId2 (PK)
        employeeName
        ---some other columns go here----


Student:
       studentId1 (PK)
       studentId2 (PK)
       studentName
       ---some other columns go here----

Address:
      addressId (PK)
      addressPersonId1 (PK & FK)
      addressPersonId2 (PK & FK)
      ---some other columns go here----

Employee 和 Student 可以有相同的地址,因此 Employee 和 Student 可以在 Address 中引用相同的记录。

这些不是我的应用程序中的实际表,但结构保持不变,我不能不惜一切代价更改表结构

我想在(员工,地址)和(学生,地址)之间实现一对一的关系。由于 PK 列的数量和 PK 列的名称不同,我无法创建关系。

将 Address(addressPersonId1, addressPersonId2) 中的列指定为主键和外键的方法是什么。以及如何从父 hbm 文件中引用这些外键。

**I have to establish mapping using HBM files but not hibernate or JPA annotations.**


**Is there any way to specify forign key column names in One to One tag in hbm file.**

提前感谢您的任何建议和解决方案。

Employee.hbm.xml
<?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.model.Employee" table="EMPLOYEE">
        <composite-id name="employeePK" class="com.model.EmployeePK">
            <key-property name="employeeId" column="EMPLOYEE_ID" />
            <key-property name="employeeName" column="EMPLOYEE_NAME" />
        </composite-id>

        <property name="employeeStatus" type="string">
            <column name="EMPLOYEE_STATUS" />
        </property>

        <one-to-one name="address" class="com.model.Address"
            cascade="all">
        </one-to-one>

    </class>
</hibernate-mapping>


Student.hbm.xml
<?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.model.Student" table="STUDENT">
        <composite-id name="studentPK" class="com.model.StudentPK">
            <key-property name="studentId" column="STUDENT_ID" />
            <key-property name="studentName" column="STUDENT_NAME" />
        </composite-id>

        <property name="studentStatus" type="string">
            <column name="STUDENT_STATUS" />
        </property>
    </class>
</hibernate-mapping>



Address.hbm.xml
<?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.model.Address" table="ADDRESS">
        <composite-id name="addressPK" class="com.model.AddressPK">
            <key-property name="addressId" column="ADDRESS_ID" />
            <key-property name="addressPersonId" column="ADDRESS_PERSON_ID" />
            <key-property name="addressPersonName" column="ADDRESS_PERSON_NAME" />
        </composite-id>

        <property name="addressStatus" type="string">
            <column name="ADDRESS_STATUS" />
        </property>
    </class>
</hibernate-mapping>
4

1 回答 1

0

尝试在一对一之前添加密钥

<one-to-one name="address" class="com.model.Address" cascade="all"></one-to-one>

变成:

<key>
    <column name="addressPK.studentCompositeId<!--<<im not sure about this, maybe you can try studentCompositeId-->" not-null="true" />
</key>
<one-to-one name="address" class="com.model.Address" cascade="all"/>

并将地址的 2 个复合键更改为 1 个复合键(您也需要编辑类),以便在 com.model.AddressPK 内部包含键 addressId 和 com.model.StudentPK 可能是这样的:

<composite-id name="addressPK" class="com.model.AddressPK">
    <key-property name="addressId" column="ADDRESS_ID" />
    <composite-id name="studentCompositeId" class="com.model.StudentPK">
        <key-property name="studentId" type="string">
            <column name="ADDRESS_PERSON_ID"/>
        </key-property>
        <key-property name="studentName" type="string">
            <column name="ADDRESS_PERSON_NAME"/>
        </key-property>
    </composite-id> 
</composite-id>
于 2013-07-23T14:09:57.183 回答