2.7.28 @ManyToMany

Organization of this subsection
(1) Description
(2) Element

(1) Description

This annotation specifies the multiple relationships from an owner entity class having a ManyToMany relationship to a non-owner entity class.

The ManyToMany relationship includes the owner and non-owner, irrespective of bi-direction or single direction. If the relationship is bi-directional, the binding table can be specified in any direction.

If the Collection element type is specified using Generics, the non-owner entity class is not required to be specified. In other cases, make sure to specify it.

Furthermore, when you specify @ManyToMany, note the settings of the following annotations:

The applicable targets are method and field.

(2) Element

The following table lists the elements of @ManyToMany:

Element nameOptional/RequiredElement description
targetEntityOptionalThis element specifies the non--owner entity class.
cascadeOptionalThis element specifies the operations to be cascaded.
fetchOptionalThis element specifies the specification value of the fetch strategy.
mappedByOptionalThis element specifies the name of the field or property that maintains a relationship in the owner entity class, when added in the elements of the non--owner entity class.

The details of attributes that are supported with Cosminexus JPA provider are as follows:

(a) targetEntity element
Type
Class
Description
This element specifies the non-owner entity class.
The specification of this element is optional when the collection property is defined using Generics. In other cases, you must always specify this element.
Default value
The type in which the collection contains parameters
# Set up only when the collection property is defined using Generics.
(b) cascade element
Type
CascadeType[]
Description
This element specifies the operations to be cascaded.
The following table describes the specifiable values:
  • CascadeType.ALL: The persist, remove, merge, and refresh operations of the owner entity class are cascaded to the related destination.
  • CascadeType.MERGE: The merge operation of the owner entity class is cascaded to the related destination.
  • CascadeType.PERSIST: The persist operation of the owner entity class is cascaded to the related destination.
  • CascadeType.REFRESH: The refresh operation of the owner entity class is cascaded to the related destination.
  • CascadeType.REMOVE: The remove operation of the owner entity class is cascaded to the related destination.
Default value
Not to be cascaded
(c) fetch element
Type
FetchType
Description
This attribute defines the fetch strategy of data from the database. For details on the fetch strategy, see 6.4.5 Synchronization with the database in the uCosminexus Application Server Common Container Functionality Guide.
The following two types of values can be specified:
  • EAGER strategy: Requests in which the data must be fetched eagerly
  • LAZY strategy: Requests in which data is fetched lazily when accessed for the first time
Default value
FetchType.LAZY
(d) mappedBy element
Type
String
Description
This element specifies the name of the field or property that maintains a relationship in the owner entity class, when added in the elements of the non-owner entity class.
When this element is specified, the relationship becomes bi-directional. For a bi-directional relationship, the value of the binding table is updated based on the information of the owner. Even when the mapping information is changed in the non--owner entity class (the entity class in which the mappedBy element is specified), the information will not be applied in the binding table.
Default value
None