001/* 002 * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved. 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 006 * which accompanies this distribution. The Eclipse Public License is available 007 * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License 008 * is available at http://www.eclipse.org/org/documents/edl-v10.php. 009 */ 010package javax.persistence; 011 012import java.lang.annotation.Retention; 013import java.lang.annotation.Target; 014 015import static java.lang.annotation.ElementType.FIELD; 016import static java.lang.annotation.ElementType.METHOD; 017import static java.lang.annotation.RetentionPolicy.RUNTIME; 018import static javax.persistence.FetchType.LAZY; 019 020/** 021 * Defines a many-valued association with one-to-many multiplicity. 022 * <p> 023 * If the collection is defined using generics to specify the element type, the associated target entity type 024 * need not be specified; otherwise the target entity class must be specified. If the relationship is 025 * bidirectional, the <code> mappedBy</code> element must be used to specify the relationship field or 026 * property of the entity that is the owner of the relationship. 027 * <p> 028 * The <code>OneToMany</code> annotation may be used within an embeddable class contained within an entity 029 * class to specify a relationship to a collection of entities. If the relationship is bidirectional, the 030 * <code> mappedBy</code> element must be used to specify the relationship field or property of the entity 031 * that is the owner of the relationship. When the collection is a <code>java.util.Map</code>, the 032 * <code>cascade</code> element and the <code>orphanRemoval</code> element apply to the map value. 033 * <p> 034 * <pre> 035 * 036 * Example 1: One-to-Many association using generics 037 * 038 * // In Customer class: 039 * 040 * @OneToMany(cascade=ALL, mappedBy="customer") 041 * public Set<Order> getOrders() { return orders; } 042 * 043 * In Order class: 044 * 045 * @ManyToOne 046 * @JoinColumn(name="CUST_ID", nullable=false) 047 * public Customer getCustomer() { return customer; } 048 * 049 * 050 * Example 2: One-to-Many association without using generics 051 * 052 * // In Customer class: 053 * 054 * @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL, 055 * mappedBy="customer") 056 * public Set getOrders() { return orders; } 057 * 058 * // In Order class: 059 * 060 * @ManyToOne 061 * @JoinColumn(name="CUST_ID", nullable=false) 062 * public Customer getCustomer() { return customer; } 063 * 064 * 065 * Example 3: Unidirectional One-to-Many association using a foreign key mapping 066 * 067 * // In Customer class: 068 * 069 * @OneToMany(orphanRemoval=true) 070 * @JoinColumn(name="CUST_ID") // join column is in table for Order 071 * public Set<Order> getOrders() {return orders;} 072 * 073 * </pre> 074 * 075 * @since Java Persistence 1.0 076 */ 077@Target({METHOD, FIELD}) 078@Retention(RUNTIME) 079public @interface OneToMany { 080 081 /** 082 * (Optional) The entity class that is the target of the association. Optional only if the collection 083 * property is defined using Java generics. Must be specified otherwise. 084 * <p> 085 * Defaults to the parameterized type of the collection when defined using generics. 086 * 087 * @return target entity 088 */ 089 Class targetEntity() default void.class; 090 091 /** 092 * (Optional) The operations that must be cascaded to the target of the association. 093 * <p> 094 * Defaults to no operations being cascaded. 095 * <p> 096 * When the target collection is a {@link java.util.Map java.util.Map}, the <code>cascade</code> element 097 * applies to the map value. 098 * 099 * @return cascade type 100 */ 101 CascadeType[] cascade() default {}; 102 103 /** 104 * (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER 105 * strategy is a requirement on the persistence provider runtime that the associated entities must be 106 * eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime. 107 * 108 * @return The fetch type 109 */ 110 FetchType fetch() default LAZY; 111 112 /** 113 * The field that owns the relationship. Required unless the relationship is unidirectional. 114 * 115 * @return mappedby 116 */ 117 String mappedBy() default ""; 118 119 /** 120 * (Optional) Whether to apply the remove operation to entities that have been removed from the 121 * relationship and to cascade the remove operation to those entities. 122 * 123 * @return whether to remove orphans 124 * @since Java Persistence 2.0 125 */ 126 boolean orphanRemoval() default false; 127}