-
@Target(value={METHOD,FIELD}) @Retention(value=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.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 ajava.util.Map
, thecascade
element and theorphanRemoval
element apply to the map value.Example 1: One-to-Many association using generics // In Customer class: @OneToMany(cascade=ALL, mappedBy="customer") public Set<Order> getOrders() { return orders; } In Order class: @ManyToOne @JoinColumn(name="CUST_ID", nullable=false) public Customer getCustomer() { return customer; } Example 2: One-to-Many association without using generics // In Customer class: @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL, mappedBy="customer") public Set getOrders() { return orders; } // In Order class: @ManyToOne @JoinColumn(name="CUST_ID", nullable=false) public Customer getCustomer() { return customer; } Example 3: Unidirectional One-to-Many association using a foreign key mapping // In Customer class: @OneToMany(orphanRemoval=true) @JoinColumn(name="CUST_ID") // join column is in table for Order public Set<Order> getOrders() {return orders;}
- Since:
- Java Persistence 1.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element and 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.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.Class
targetEntity
(Optional) The entity class that is the target of the association.
-
-
-
Element Detail
-
targetEntity
public abstract 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
public abstract 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
java.util.Map
, thecascade
element applies to the map value.- Default:
- {}
-
-
-
fetch
public abstract 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:
- javax.persistence.FetchType.LAZY
-
-
-
mappedBy
public abstract String mappedBy
The field that owns the relationship. Required unless the relationship is unidirectional.- Default:
- ""
-
-
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 11/06/2005, zuletzt geändert 18/08/2025
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-javaee-rf-javax/persistence/onetomany.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor dieser Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.