Annotation Interface ManyToMany
targetEntity() must be explicitly specified.
The optional JoinTable annotation specifies the mapped join
table.
@ManyToMany
@JoinTable(name = "BOOK_AUTHOR",
joinColumns = @JoinColumn(name = "BOOK_ISBN",
referencedColumnName = "ISBN"),
inverseJoinColumns = @JoinColumn(name = "AUTHOR_SSN",
referencedColumnName = "SSN"))
Set<Author> authors;
The annotated field or property might represent one side of a bidirectional association. Every bidirectional association has an owning side and an inverse (alternatively, non-owning or unowned) side. Modifications to the owning side of an association determine the updates made to the relationship in the database. If the inverse side of an association is modified without a corresponding modification to the owning side, the behavior is undefined. The persistence provider is permitted to ignore any modification made only to the inverse side of a bidirectional association.
The inverse side of a bidirectional ManyToMany association
must be a field or property also annotated @ManyToMany of the
target entity, and the inverse side must
specify the owning relationship field or property via mappedBy().
The join table must be specified on the owning side.
@Entity
public class Book {
@Id
String isbn;
// owning side
@ManyToMany
@JoinTable(name = "BOOK_AUTHOR")
Set<Author> authors;
...
}
@Entity
public class Author {
@Id
String ssn;
// inverse (unowned) side
@ManyToMany(mappedBy = Book_.AUTHORS)
Set<Customer> customers;
...
}
The ManyToMany annotation may be used within an
embeddable class contained within an entity class to specify a
relationship to a collection of entities. If the relationship is
bidirectional and the entity containing the embeddable class is
the owner of the relationship, the non-owning side must use the
mappedBy() element of the ManyToMany annotation to
specify the relationship field or property of the embeddable class.
The dot (.) notation syntax must be used in the
mappedBy() element to indicate the relationship attribute
within the embedded attribute. The value of each identifier used
with the dot notation is the name of the respective embedded field
or property.
- Since:
- 1.0
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescription(Optional) The operations that must be cascaded to the target of the association.(Optional) Whether the association should be lazily loaded or must be eagerly fetched.The field or property of the target entity that owns the relationship and specifies its mapping to the database.Class<?> (Optional) The entity class that is the target of the association.
-
Element Details
-
targetEntity
Class<?> targetEntity(Optional) The entity class that is the target of the association. Required if the target entity type cannot be inferred from the declared type of the annotated field or property, for example, if the declared type is a raw collection type.Defaults to the type argument of the collection type when the declared type of the annotated field or property is a non-raw collection type.
- Default:
void.class
-
cascade
CascadeType[] cascade(Optional) The operations that must be cascaded to the target of the association.When the target collection is a
Map, thecascadeelement applies to the map value.By default, no operations are cascaded.
- Default:
{}
-
fetch
FetchType fetch(Optional) Whether the association should be lazily loaded or must be eagerly fetched.- The
EAGERpolicy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. - The
LAZYpolicy is a hint to the persistence provider runtime.
If not specified, defaults to
LAZY.- Default:
LAZY
- The
-
mappedBy
String mappedByThe field or property of the target entity that owns the relationship and specifies its mapping to the database. This element is only specified on the inverse (non-owning) side of the association.The static metamodel of the target entity may be used to obtain a reference to the owning side, for example,
mappedBy = Book_.AUTHORS.- Default:
""
-