0

I have two classes Employee and Project, i am doing one to one relationship and it is working without property-ref please see below:

<one-to-one name="project" class="com.hibernate.practice.entitytype.Project" <!--property-ref="employee"--> cascade="save-update"> </one-to-one>

as u can see i have commented the property-ref

Other side of project (class) mapping file i have defined:

<many-to-one name="employee" class="com.hibernate.practice.entitytype.Employee" column="EMP_ID" unique="true" ></many-to-one>

now what the definition says : property-ref (optional): the name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

I have not specified "property-ref" but still EMP_ID column in project table is populated with the Employee ID(primary key) i was expecting it will be populated with Project ID(primary key) as the definition says (highlighted in bold). Please explain what is wrong, when i add it or do not add this property-ref, I can't see any difference ? Why it is in hibernate? Please i am waiting for an answer.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
asad khan
  • 65
  • 1
  • 3
  • 9

1 Answers1

1

but still EMP_ID column in project table is populated with the Employee ID(primary key). I was expecting it will be populated with Project ID(primary key) as the definition says (highlighted in bold).

The Project.EMP_ID should be populated with Employee.ID. That's how foreign keys work.

I am not sure why you wanted the Project.EMP_ID to point to Project.ID.

If the Project and Employee don't share the primary key:

<one-to-one name="person" class="Person"/>

<one-to-one name="employee" class="Employee" constrained="true"/>

you need to use property-ref for the inverse side of the directional association:

<one-to-one name="employee" class="Employee" property-ref="person"/>

In the second example, you simply have only one FK in the child table pointing to the parent table. In this case, you need to instruct Hibernate which side of the association is the owning one (most often it's the child side one).

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I know how foreign key work :) , my question is when to use property-ref, So u said that when u having parent-child relationship like Person and employee then u use property-ref in child side means child own the relationship, right ? but in my example there is no inheritance relationship like Employee and Project(assign to employee) so is it necessary to use here property-ref or not i saw a video tutorial of Hibernate in Pluralsight in that it use this property-ref in a example and for practice i removed this property it is still working fine that's why i asked here – asad khan Jan 16 '15 at 10:12
  • Without property-ref the link is done using the child.id instead of child.parent_id. So it depends on how you want the mapping to be done – Vlad Mihalcea Jan 16 '15 at 10:17