-
Notifications
You must be signed in to change notification settings - Fork 2
OneToOne
The OneToOne annotation is used to define a one-to-one relationship between two domain entities. The annotation is placed in each class which make up the relationship on the field which models it. For example, if class Foo has a one-to-one relationship with class Bar, Foo will contain a Bar field and vice versa. The annotation would then be placed on each of these fields in the respective classes.
OneToOne has four required attributes that are used to define the relationship and how it should be represented.
column: indicates the name of the column representing the foreign key in this relationship. This column needs to be non-null and unique to maintain one-to-one integrity.
owner: indicates the owner of the relationship. This is the class which contains the foreign key.
name: indicates the name of the relationship. Names are used to uniquely identify relationships within a class and provide descriptive metadata.
The code below shows how a one-to-one relationship is established between the two classes Manager and Department. Let's assume that a Manager manages exactly one Department and a Department has exactly one Manager.
public class Manager extends Employee {
@OneToOne(name="manages", column="dept", owner=Manager.class)
private Department mDepartment;
// ...
}
public class Department {
private String mName;
@OneToOne(name="managedBy", column="dept", owner=Manager.class)
private Manager mManager;
// ...
}The example above establishes a one-to-one relationship between Manager and Department. The column dept in the Manager table acts as a foreign key referencing the Department a Manager is responsible for. Since this is a one-to-one relationship, this column is unique and not nullable.