0

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>
  • In case of One To Many, we can specify mapping column names in parent hbm file as ** ** Is there any way to specify this in One to One relationship. – Sandeep Goud Karrey Jul 23 '13 at 05:30
  • ` ` the `` is your foreign key in other table – Angga Jul 23 '13 at 05:46
  • **I cant change the tables structure at any cost** << this means you cant add another FK? can i assume that you have 4 PK that want to be mapped to 2 FK on the address? – Angga Jul 23 '13 at 06:20
  • yes, both Employee(employeeId1,employeeId2) and Student(studentId1,studentId2) primary keys must be mapped to FK's(addressPersonId1, addressPersonId2) of Address table. – Sandeep Goud Karrey Jul 23 '13 at 06:49
  • HI Angga, tag has raised an exception **ERROR: HHH000196: Error parsing XML (2) : The content of element type "one-to-one" must match "(meta*,formula*)". Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Unable to read XML** – Sandeep Goud Karrey Jul 23 '13 at 06:56
  • why did you join the name to became fk? you cant do that, post your xml here so we can fix it – Angga Jul 23 '13 at 07:05
  • Hi, I have added hbm files to question, have a look... – Sandeep Goud Karrey Jul 23 '13 at 07:55

1 Answers1

0

try add key before your one-to-one

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

became :

<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"/>

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:

<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>
Angga
  • 2,305
  • 1
  • 17
  • 21