I have two parent tables and one child table, with composite key's mentioned below:
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 and Student can have same address, so Employee and Student can refer to same record in Address.
These are not actual table that I have in my application, but structure remains same, I cant change the tables structure at any cost.
I want to achive One to One relationship between (Employee, Address) and (Student, Address). As the number of PK columns and Names of PK columns are not same, I am unable to create the relationship.
what is the approach to specify columns in Address(addressPersonId1, addressPersonId2) as Primary and Forign key's.And how can I refer these forign keys from Parent hbm files.
**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.**
Thanks in advance for any suggestions and solutions.
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>
try add key before your one-to-one
became :
and give change the 2 composite key of Address to became 1 composite(you will need to edit the class too) so that inside com.model.AddressPK contains keys addressId and com.model.StudentPK something like this maybe: