OneToOne unidirectional and bidirectional Mapping
It is very easy to find entities with relationships. A person has dogs, dogs have fleas, fleas has… hum… never mind.
Unidirectional
A one to one relationship is the easiest to understand. Imagine that a Person has only one Cellular and only Person will “see” the Cellular, the Cellular will not see Person. Check the image below:
import javax.persistence.*;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
@OneToOne
@JoinColumn(name="cellular_id")
private Cellular cellular;
// get and set
}
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
private int number;
// get and set
}
About the code above:
•In a unidirectional relationship just one side of the relationship knows (“sees”) the other. Notice
that Person knows Cellular but Cellular does not know Person. It is possible to do
person.getCellular() but it is not possible to do cellular.getPerson().
•In the Person entity it is possible to use the annotation @OneToOne. This annotation indicates to
JPA that there is a relationship between the entities.
Every relationship needs one of the entities to be the “relationship owner”. Being the relationship owner is nothing more than to have the foreign key in the database table. In the code above you can see that the annotation @JoinColumn has been used. This annotation indicates that the foreign key will be located in the person database table, making the Person entity owner of the relationship.
It is very easy to find entities with relationships. A person has dogs, dogs have fleas, fleas has… hum… never mind.
Unidirectional
A one to one relationship is the easiest to understand. Imagine that a Person has only one Cellular and only Person will “see” the Cellular, the Cellular will not see Person. Check the image below:
import javax.persistence.*;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
@OneToOne
@JoinColumn(name="cellular_id")
private Cellular cellular;
// get and set
}
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
private int number;
// get and set
}
About the code above:
•In a unidirectional relationship just one side of the relationship knows (“sees”) the other. Notice
that Person knows Cellular but Cellular does not know Person. It is possible to do
person.getCellular() but it is not possible to do cellular.getPerson().
•In the Person entity it is possible to use the annotation @OneToOne. This annotation indicates to
JPA that there is a relationship between the entities.
Every relationship needs one of the entities to be the “relationship owner”. Being the relationship owner is nothing more than to have the foreign key in the database table. In the code above you can see that the annotation @JoinColumn has been used. This annotation indicates that the foreign key will be located in the person database table, making the Person entity owner of the relationship.
No comments:
Post a Comment