Bidirectional Mapping
To transform this relationship in a bidirectional one we just have to edit the Cellular entity. Check the class below:
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
private int number;
@OneToOne(mappedBy="cellular")
private Person person;
// get and set
}
About the code above:
•The same annotation @OneToOne to the Person attribute is used in the Cellular entity.
•The parameter “mappedBy” was used in the @OneToOnea nnotation. This parameter indicates
that the entity Person is the owner of the relationship; the foreign key must exist inside the
person table, and not the Cellular table.
A developer must have in mind that for JPA to work in an optimal way it is a good practice to leave one side of the relationship as the owner. If the annotation @OneToOne found in the Cellular entity was without the “mappedBy” parameter, JPA would handle the Cellular entity as the owner of the relationship too. It is not a good idea to leave either sides of a relationship without defining "mappedBy", or both with "mappedBy" setted.
There is no such “auto relationship”
For a Bidirectional relationship to work correctly it is necessary to do like below:
person.setCellular(cellular);
cellular.setPerson(person);
JPA uses the Java concept of class reference, a class must maintain a reference to another one if there will be a join between them. JPA will not create a relationship automatically; to have the relationship in both sides it is needed to do like above.
NEXT
To transform this relationship in a bidirectional one we just have to edit the Cellular entity. Check the class below:
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
private int number;
@OneToOne(mappedBy="cellular")
private Person person;
// get and set
}
About the code above:
•The same annotation @OneToOne to the Person attribute is used in the Cellular entity.
•The parameter “mappedBy” was used in the @OneToOnea nnotation. This parameter indicates
that the entity Person is the owner of the relationship; the foreign key must exist inside the
person table, and not the Cellular table.
A developer must have in mind that for JPA to work in an optimal way it is a good practice to leave one side of the relationship as the owner. If the annotation @OneToOne found in the Cellular entity was without the “mappedBy” parameter, JPA would handle the Cellular entity as the owner of the relationship too. It is not a good idea to leave either sides of a relationship without defining "mappedBy", or both with "mappedBy" setted.
There is no such “auto relationship”
For a Bidirectional relationship to work correctly it is necessary to do like below:
person.setCellular(cellular);
cellular.setPerson(person);
JPA uses the Java concept of class reference, a class must maintain a reference to another one if there will be a join between them. JPA will not create a relationship automatically; to have the relationship in both sides it is needed to do like above.
NEXT
No comments:
Post a Comment