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 many-to-many multiplicity. 021 * <p> 022 * Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The 023 * join table is specified on the owning side. If the association is bidirectional, either side may be 024 * designated as the owning side. If the relationship is bidirectional, the non-owning side must use the 025 * <code>mappedBy</code> element of the <code>ManyToMany</code> annotation to specify the relationship field 026 * or property of the owning side. 027 * <p> 028 * The join table for the relationship, if not defaulted, is specified on the owning side. 029 * <p> 030 * The <code>ManyToMany</code> annotation may be used within an embeddable class contained within an entity 031 * class to specify a relationship to a collection of entities. If the relationship is bidirectional and the 032 * entity containing the embeddable class is the owner of the relationship, the non-owning side must use the 033 * <code>mappedBy</code> element of the <code>ManyToMany</code> annotation to specify the relationship field 034 * or property of the embeddable class. The dot (".") notation syntax must be used in the 035 * <code>mappedBy</code> element to indicate the relationship attribute within the embedded attribute. The 036 * value of each identifier used with the dot notation is the name of the respective embedded field or 037 * property. 038 * <p> 039 * <pre> 040 * 041 * Example 1: 042 * 043 * // In Customer class: 044 * 045 * @ManyToMany 046 * @JoinTable(name="CUST_PHONES") 047 * Set<PhoneNumber> phones; 048 * 049 * // In PhoneNumber class: 050 * 051 * @ManyToMany(mappedBy="phones") 052 * Set<Customer> customers; 053 * 054 * Example 2: 055 * 056 * // In Customer class: 057 * 058 * @ManyToMany(targetEntity=com.acme.PhoneNumber.class) 059 * Set phones; 060 * 061 * // In PhoneNumber class: 062 * 063 * @ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones") 064 * Set customers; 065 * 066 * Example 3: 067 * 068 * // In Customer class: 069 * 070 * @ManyToMany 071 * @JoinTable(name="CUST_PHONE", 072 * joinColumns= 073 * @JoinColumn(name="CUST_ID", referencedColumnName="ID"), 074 * inverseJoinColumns= 075 * @JoinColumn(name="PHONE_ID", referencedColumnName="ID") 076 * ) 077 * Set<PhoneNumber> phones; 078 * 079 * // In PhoneNumberClass: 080 * 081 * @ManyToMany(mappedBy="phones") 082 * Set<Customer> customers; 083 * </pre> 084 * 085 * @see JoinTable 086 * @since Java Persistence 1.0 087 */ 088@Target({FIELD}) 089@Retention(RUNTIME) 090public @interface ManyToMany { 091 092 /** 093 * (Optional) The entity class that is the target of the association. Optional only if the 094 * collection-valued relationship property is defined using Java generics. Must be specified otherwise. 095 * <p> 096 * Defaults to the parameterized type of the collection when defined using generics. 097 * 098 * @return The target entity 099 */ 100 Class targetEntity() default void.class; 101 102 /** 103 * (Optional) The operations that must be cascaded to the target of the association. 104 * <p> 105 * When the target collection is a {@link java.util.Map java.util.Map}, the <code>cascade</code> element 106 * applies to the map value. 107 * <p> 108 * Defaults to no operations being cascaded. 109 * 110 * @return The cascade type 111 */ 112 CascadeType[] cascade() default {}; 113 114 /** 115 * (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER 116 * strategy is a requirement on the persistence provider runtime that the associated entities must be 117 * eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime. 118 * 119 * @return The fetch type 120 */ 121 FetchType fetch() default LAZY; 122 123 /** 124 * The field that owns the relationship. Required unless the relationship is unidirectional. 125 * 126 * @return The mappedby 127 */ 128 String mappedBy() default ""; 129}