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