KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > persistence > ManyToMany


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package javax.persistence;
24
25 import java.lang.annotation.Target JavaDoc;
26 import java.lang.annotation.Retention JavaDoc;
27 import javax.persistence.CascadeType;
28 import static java.lang.annotation.ElementType.FIELD JavaDoc;
29 import static java.lang.annotation.ElementType.METHOD JavaDoc;
30 import static java.lang.annotation.RetentionPolicy.RUNTIME JavaDoc;
31 import static javax.persistence.FetchType.LAZY;
32
33 /**
34  * Defines a many-valued association with many-to-many multiplicity.
35  * If the Collection is defined using generics to specify the element
36  * type, the associated target entity class does not need to be
37  * specified; otherwise it must be specified.
38  *
39  * <p> Every many-to-many association has two sides, the owning
40  * side and the non-owning, or inverse, side. The join table is
41  * specified on the owning side. If the association is bidirectional,
42  * either side may be designated as the owning side.
43  *
44  * <p> The same annotation elements for the {@link OneToMany}
45  * annotation apply to the <code>ManyToMany</code> annotation.
46  *
47  * <pre>
48  *
49  * Example 1:
50  *
51  * In Customer class:
52  *
53  * &#064;ManyToMany
54  * &#064;JoinTable(name="CUST_PHONES")
55  * public Set<PhoneNumber> getPhones() { return phones; }
56  *
57  * In PhoneNumber class:
58  *
59  * &#064;ManyToMany(mappedBy="phones")
60  * public Set<Customer> getCustomers() { return customers; }
61  *
62  * Example 2:
63  *
64  * In Customer class:
65  *
66  * &#064;ManyToMany(targetEntity=com.acme.PhoneNumber.class)
67  * public Set getPhones() { return phones; }
68  *
69  * In PhoneNumber class:
70  *
71  * &#064;ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
72  * public Set getCustomers() { return customers; }
73  *
74  * Example 3:
75  *
76  * In Customer class:
77  *
78  * &#064;ManyToMany
79  * &#064;JoinTable(name="CUST_PHONE",
80  * joinColumns=
81  * &#064;JoinColumn(name="CUST_ID", referencedColumnName="ID"),
82  * inverseJoinColumns=
83  * &#064;JoinColumn(name="PHONE_ID", referencedColumnName="ID")
84  * )
85  * public Set<PhoneNumber> getPhones() { return phones; }
86  *
87  * In PhoneNumberClass:
88  *
89  * &#064;ManyToMany(mappedBy="phones")
90  * public Set<Customer> getCustomers() { return customers; }
91  * </pre>
92  *
93  * @since Java Persistence 1.0
94  */

95 @Target JavaDoc({METHOD, FIELD})
96 @Retention JavaDoc(RUNTIME)
97
98 public @interface ManyToMany {
99
100     /**
101      * (Optional) The entity class that is the target
102      * of the association. Optional only if the collection
103      * property is defined using Java generics.
104      * Must be specified otherwise.
105      *
106      * <p> Defaults to the parameterized type of
107      * the collection when defined using generics.
108      */

109     Class JavaDoc targetEntity() default void.class;
110
111     /**
112      * (Optional) The operations that must be cascaded to
113      * the target of the association.
114      * <p> Defaults to no operations being cascaded.
115      */

116     CascadeType[] cascade() default {};
117
118     /** (Optional) Whether the association should be
119      * lazily loaded or must be eagerly fetched. The
120      * {@link FetchType#EAGER EAGER} strategy is a
121      * requirement on the persistenceprovider runtime
122      * that the associatedentities must be eagerly fetched.
123      * The {@link FetchType#LAZY LAZY} strategy is a hint
124      * to the persistence provider runtime.
125      */

126     FetchType fetch() default LAZY;
127
128     /**
129      * The field that owns the relationship. Required unless
130      * the relationship is unidirectional.
131      */

132     String JavaDoc mappedBy() default "";
133 }
134
Popular Tags