-
@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface OneToMany
Specifies a many-valued association with one-to-many multiplicity.If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified. If the relationship is bidirectional, the
mappedBy()
element must be used to specify the relationship field or property of the entity that is the owner of the relationship.A
OneToMany
association usually maps a foreign key column or columns in the table of the associated entity. This mapping may be specified using theJoinColumn
annotation. Alternatively, a unidirectionalOneToMany
association is sometimes mapped to a join table using theJoinTable
annotation.The
OneToMany
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, themappedBy()
element must be used to specify the relationship field or property of the entity that is the owner of the relationship.When the collection is a
Map
, thecascade()
element and theorphanRemoval()
element apply to the map value.Example 1: One-to-Many association using generics {@snippet : // In Customer class:
- Since:
- 1.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description CascadeType[]
cascade
(Optional) The operations that must be cascaded to the target of the association.FetchType
fetch
(Optional) Whether the association should be lazily loaded or must be eagerly fetched.java.lang.String
mappedBy
The field that owns the relationship.boolean
orphanRemoval
(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.java.lang.Class<?>
targetEntity
(Optional) The entity class that is the target of the association.
-
-
-
Element Detail
-
targetEntity
java.lang.Class<?> targetEntity
(Optional) The entity class that is the target of the association. Optional only if the collection property is defined using Java generics. Must be specified otherwise.Defaults to the parameterized type of the collection when defined using generics.
- Default:
- void.class
-
-
-
cascade
CascadeType[] cascade
(Optional) The operations that must be cascaded to the target of the association.Defaults to no operations being cascaded.
When the target collection is a
Map
, thecascade
element applies to the map value.- Default:
- {}
-
-
-
fetch
FetchType fetch
(Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the associated entities must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.- Default:
- jakarta.persistence.FetchType.LAZY
-
-