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?
Hello everybody i wonder: when can i use mapped by to indicate whose is
Share
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
mappedByelement.From the JPA 1.0 specification:
Imagine the following model:
And
In this example, the
mappedByattribute shows that aPlayerinstance’steamproperty maps to theTeaminstance and theTeamobject’s identifier will exist as a foreign key column in the PLAYER table. The owningPlayerside of the relationship is responsible for storing the foreign key.If the
mappedByis 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
References