OneToMany/ManyToOne unidirectional and bidirectional
The one to many relationship is used when an entity has a relationship with a list of other entities, e.g. a Cellular
may have several Calls but a Call can have only one Cellular. The OneToMany relationship is represented with a
list of values, there is more than one entity associate with it.
Let us start with the ManyToOne side:
import javax.persistence.*;
@Entity
public class Call {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "cellular_id")
private Cellular cellular;
private long duration;
// get and set
}
About the code above:
•The annotation @ManyToOne is used.
•Notice that annotation @JoinColumn is used to define who the owner of the relationship is.
•The @ManyToOne side will always be the owner of the relationship. There is no way of using the
mappedBy attribute inside the @ManyToOne annotation.
To do a bidirectional relationship we need to edit the Cellular entity (created in the previous sections). Check thecode below:
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
@OneToOne(mappedBy = "cellular")
private Person person;
@OneToMany(mappedBy = "cellular")
private List<Call> calls;
private int number;
// get and set
}
About the code above:
•The @OneToMany annotation is used. This annotation must be placed in a collection.
•The mappedBy directtive is used to define the Call entity as the relationship owner.
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 call database
table, making the Call entity owner of the relationship.
There is no such “auto relationship”
For a Bidirectional relationship to work correctly it is necessary to do like below:
call.setCellular(cellular);
cellular.setCalls(calls);
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
The one to many relationship is used when an entity has a relationship with a list of other entities, e.g. a Cellular
may have several Calls but a Call can have only one Cellular. The OneToMany relationship is represented with a
list of values, there is more than one entity associate with it.
Let us start with the ManyToOne side:
import javax.persistence.*;
@Entity
public class Call {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "cellular_id")
private Cellular cellular;
private long duration;
// get and set
}
About the code above:
•The annotation @ManyToOne is used.
•Notice that annotation @JoinColumn is used to define who the owner of the relationship is.
•The @ManyToOne side will always be the owner of the relationship. There is no way of using the
mappedBy attribute inside the @ManyToOne annotation.
To do a bidirectional relationship we need to edit the Cellular entity (created in the previous sections). Check thecode below:
import javax.persistence.*;
@Entity
public class Cellular {
@Id
@GeneratedValue
private int id;
@OneToOne(mappedBy = "cellular")
private Person person;
@OneToMany(mappedBy = "cellular")
private List<Call> calls;
private int number;
// get and set
}
About the code above:
•The @OneToMany annotation is used. This annotation must be placed in a collection.
•The mappedBy directtive is used to define the Call entity as the relationship owner.
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 call database
table, making the Call entity owner of the relationship.
There is no such “auto relationship”
For a Bidirectional relationship to work correctly it is necessary to do like below:
call.setCellular(cellular);
cellular.setCalls(calls);
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