我有两个父表和一个子表,下面提到了复合键:
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----
员工和学生可以拥有相同的地址,因此员工和学生可以在地址中引用相同的记录。
这些不是我在我的应用程序中的实际表格,但结构保持不变, 我无法不惜一切代价更改表格结构 。
我想在(员工,地址)和(学生,地址)之间建立一对一的关系。 由于PK列的数量和PK列的名称不相同,我无法创建关系。
将Address(addressPersonId1,addressPersonId2)中的列指定为Primary和Forign键的方法是什么。如何从Parent hbm文件中引用这些forign键。
**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>
答案 0 :(得分: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>