Hello everybody i wonder: when can i use mapped by to indicate whose is owing of relationship in one-to -one or one to many - or many to many relationship mapping with EJB3 (JPA) example i have two table A and B table A belong to table B so what i place mapped by for whose table?
The mappedBy attribute can be used when the relationship is already define on the other part.
For example, in a One to One relationship between entities A and B:
@Entity
public class A {
@OneToOne
@JoinColumn
private B b;
// Code removed for clarity
}
Here, my A class defines the relationship to B. It is a One to One relationship using a join column. If I want B to be linked to A, using this already defined relationship, to establish a bidirectional relationship between my entities, I can use the mappedBy attribute :
@Entity
public class B {
@OneToOne(mappedBy="b")
private A a;
// Code removed for clarity
}
Here, the mappedBy attribute means "I want to defined a One to One relationship, which has already been defined on entity A, on the attribute called 'b'".
In general, the owning side of a bidirectional relationship is the entity mapped to the table containing the join column referencing the other table. In case you are using a join table, any side can be the owning side, it just has to make sense in your data model.
when can I use mapped by to indicate whose is owing of relationship in one-to -one or one to many - or many to many relationship mapping with EJB3
A relationship can be unidirectional or bidirectional. Within a bidirectional relationship you must specify the owning side of the relationship in the other class with the mappedBy
element.
- The owning side which is responsible for propagating the update of the relationship to the database. Usually this is the side with the foreign key.
- The inverse side maps to the owning side.
From the JPA 1.0 specification:
2.1.7 Entity Relationships ...
Relationships may be bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side. The owning side of a relationship determines the updates to the relationship in the database, as described in section 3.2.3.
The following rules apply to bidirectional relationships:
- The inverse side of a bidirectional relationship must refer to its owning side by use of the
mappedBy
element of theOneToOne
,OneToMany
, orManyToMany
annotation. ThemappedBy
element designates the property or field in the entity that is the owner of the relationship.- The many side of one-to-many / many-to-one bidirectional relationships must be the owning side, hence the
mappedBy
element cannot be specified on theManyToOne
annotation.- For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.
- For many-to-many bidirectional relationships either side may be the owning side.
Imagine the following model:
@Entity
public class Player {
...
private Team team;
@ManyToOne
public Team getTeam() { return team; }
...
}
And
@Entity
public class Team {
...
private Set<Player> players = new HashSet<Player();
public Team() { }
@OneToMany(mappedBy = "team")
public Set<Player> getPlayers() { return players; }
...
}
In this example, the mappedBy
attribute shows that a Player
instance's team
property maps to the Team
instance and the Team
object's identifier will exist as a foreign key column in the PLAYER table. The owning Player
side of the relationship is responsible for storing the foreign key.
If the mappedBy
is not used, the persistence provider will assume that there are two independent relationships:
Which is generally not what you want and might end up getting unexpected behavior (e.g. duplicate rows inserted with many-to-many).
Related questions
- In a bidirectional JPA OneToMany/ManyToOne association, what is meant by “the inverse side of the association”?
- JPA: which side should be the owning side in a m:n relationship?
References
- JPA 1.0 specification
- Section 2.1.7 "Entity Relationships"