Annotation Interface ManyToMany
This annotation also adds the @Transient and
@Value("null") annotations to the field.
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanWhether "orphan" entities are deleted or not.Specifies the name of the join table responsible for the many-to-many relationship between two tables.Specifies the name of the "foreign key" column that maps the join table with the linked table.booleanWhether the associated entities are only linked to the join table or not.Specifies the name of the "foreign key" column that maps the annotated field's entity with the join table.booleanWhether the associated entities are read-only or not, meaning they are never persisted or linked.Specifies the column used to sort the populated associated entities.org.springframework.data.domain.Sort.DirectionSpecifies in which direction the populated children entities are sorted.
-
Element Details
-
deleteOrphans
boolean deleteOrphansWhether "orphan" entities are deleted or not. Defaults tofalse.Usually, many-to-many relationships are not mutually exclusive to each other, meaning that one can exist without the other even when they are not linked in their join table. In this context, "orphans" refers to all entities no longer linked to the current entity. By default, the annotation will only delete the links to the "orphan" entities in the join table. Setting this option to
truewill also delete the "orphan" entities.- Returns:
trueif "orphan" entities are deleted,falseotherwise- ApiNote:
- given the nature of many-to-many relationships, setting this
option to
trueis highly discouraged as it can produce unexpected results, especially in bidirectional associations
- Default:
false
-
joinTable
String joinTableSpecifies the name of the join table responsible for the many-to-many relationship between two tables. This is optional if the name of the join table matches the names of both related tables joined by an underscore (in any order).For example, given a table
authorand a tablebook, the join table for the relationship can either be inferred asauthor_bookor asbook_author.- Returns:
- the name of the relationship join table
- Default:
""
-
linkedBy
String linkedBySpecifies the name of the "foreign key" column that maps the join table with the linked table. This is optional if the column's name matches the linked table name followed by an_idsuffix.For example, given a table
authorand a tablebook, and given the annotation is used in a field ofauthor's entity, we can say the linked table isbookand its "foreign key" column in the join table is inferred asbook_id.- Returns:
- the name of the column linking the join table
- Default:
""
-
linkOnly
boolean linkOnlyWhether the associated entities are only linked to the join table or not. Defaults tofalse.Link-only means the associated entities already exist. The annotation will only create the link in the join table column when required. The associated entities are never updated.
- Returns:
trueif the associated entities are link-only,falseotherwise
- Default:
false
-
mappedBy
String mappedBySpecifies the name of the "foreign key" column that maps the annotated field's entity with the join table. This is optional if the column's name matches the entity's table name followed by an_idsuffix.For example, given a table
authorand a tablebook, and given the annotation is used in a field ofauthor's entity, the "foreign key" column in the join table is inferred asauthor_id.- Returns:
- the name of the column mapping the join table
- Default:
""
-
readonly
boolean readonlyWhether the associated entities are read-only or not, meaning they are never persisted or linked. Defaults tofalse.- Returns:
trueif the associated entities are read-only,falseotherwise
- Default:
false
-
sortBy
String sortBySpecifies the column used to sort the populated associated entities.By default, the annotation tries to find the field associated with
Auditable.getCreatedDate()or annotated with@CreatedDate. If none can be found, it'll try to find a"created_at"column as a last resort. If all of that fails, the children will be unsorted.- Returns:
- the sorting column name
- Default:
""
-
sortIn
org.springframework.data.domain.Sort.Direction sortInSpecifies in which direction the populated children entities are sorted. Defaults toSort.Direction.DESC.- Returns:
- the sort direction
- Default:
DESC
-