8.12.36 @OneToMany
- Organization of this subsection
(1) Description
This annotation specifies the multiple relationships from an owner entity class having the OneToMany relationship to a non-owner entity class.
The elements of the same annotations for @OneToMany are same as that of @ManyToMany.
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, to achieve a bi-directional relationship, always specify the mappedBy element at the non-owner side.
The applicable targets are method and field.
(2) Element
The following table lists the elements of @OneToMany:
Element name |
Optional/Required |
Element description |
---|---|---|
Optional |
This element specifies the non-owner entity class. |
|
Optional |
This element specifies the operations to be cascaded. |
|
Optional |
This element specifies the specification value of the fetch strategy. |
|
Optional |
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. |
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 a 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
Note: 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 database. For details on the fetch strategy, see 9.4.5 Synchronization with the database.
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.
- Default value
-
None