Uni-direction: only one class has a reference
Bidirection: both classes have the reference
1.Annotation
Uni-direction: no change to wife; wife_id is used as a foreign key to Husband;
@JoinColumn(name="name of the column in DB"), if we dont add this, by default, the column's name would be wife_id
@Entity public class Husband { private int id; private String name; private Wife wi; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @OneToOne //name of the column if you want is different than default @JoinColumn(name="wifeId") public Wife getWife() { return wi; } public void setWife(Wife wife) { wi = wife; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Bidirection: we add (mappedBy ="wife") to Wife class;in such way, we won't have husband_id in Wife class, only wife_id is in husband class.
@Entity public class Wife { private int id; private String name; private Husband husband; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } //="wife" b/c the method is called getWife, we take"wife" after get ;not b/c private Wife wife @OneToOne(mappedBy="wife") public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.XML
Unidirection:
Nothing special to Student class; StudentIdCard class has "id" from Student class as a foreign key
<many-to-one> should be on "many" side. StudentIDCard.hbm.xml should have <many-to-one name="student of getStudent()">; column ="" is optional;
We must have unique=true to keep the foreign key one-to-one relationship
<class name="Student" table="student" dynamic-update="true"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age"></property> <property name="classID"></property> </class> <class name="com.hibernate.model.StuIdCard"> <id name="id"> <generator class="native"> </generator> </id> <property name="num"></property> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class>
Bidirection:
Student class: contains<one-to-one> tag as well, in order to avoid foreign key in both classes, we add <property-ref ="xx">(xx is the Student variable in studentID class);
StuIdCard class: no change , same as uidirection;
<class name="Student" table="student" dynamic-update="true"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age"></property> <property name="classID"></property> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> <class name="com.hibernate.model.StuIdCard"> <id name="id"> <generator class="foreign"> <param name="property">student</param> </generator> </id> <property name="num"></property>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>