KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > metadata > accessors > ManyToManyAccessor


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors;
23
24 import javax.persistence.ManyToMany;
25
26 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor;
27 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject;
28
29 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper;
30
31 import oracle.toplink.essentials.mappings.ManyToManyMapping;
32
33 /**
34  * A many to many relationship accessor.
35  *
36  * @author Guy Pelletier
37  * @since TopLink EJB 3.0 Reference Implementation
38  */

39 public class ManyToManyAccessor extends CollectionAccessor {
40     /**
41      * INTERNAL:
42      */

43     public ManyToManyAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) {
44         super(accessibleObject, classAccessor);
45         
46         ManyToMany manyToMany = getAnnotation(ManyToMany.class);
47
48         if (manyToMany != null) {
49             setFetchType(manyToMany.fetch());
50             setMappedBy(manyToMany.mappedBy());
51             setCascadeTypes(manyToMany.cascade());
52             setTargetEntity(manyToMany.targetEntity());
53         }
54     }
55     
56     /**
57      * INTERNAL:
58      */

59     public boolean isManyToMany() {
60         return true;
61     }
62     
63     /**
64      * INTERNAL:
65      * Process a @ManyToMany or many-to-many element into a TopLink MnayToMany
66      * mapping.
67      */

68     public void process() {
69         // Create and initialize our mapping.
70
ManyToManyMapping mapping = new ManyToManyMapping();
71         populateCollectionMapping(mapping, m_logger.MANY_TO_MANY_MAPPING_REFERENCE_CLASS);
72
73         if (m_mappedBy.equals("")) {
74             // Processing the owning side of a M-M that is process a join table.
75
processJoinTable(getJoinTable(), mapping);
76         } else {
77             // We are processing the a non-owning side of a M-M. Must set the
78
// mapping read-only.
79
mapping.setIsReadOnly(true);
80             
81             // Get the owning mapping from the reference descriptor metadata.
82
ManyToManyMapping ownerMapping = null;
83             if (getOwningMapping().isManyToManyMapping()){
84                 ownerMapping = (ManyToManyMapping)getOwningMapping();
85             }else {
86                 //gf730 - If improper mapping encountered, throw an exception
87
getValidator().throwIvalidMappingEncountered(getJavaClass(), getReferenceClass());
88             }
89
90             // Set the relation table name from the owner.
91
mapping.setRelationTableName(ownerMapping.getRelationTableName());
92                  
93             // Add all the source foreign keys we found on the owner.
94
mapping.setSourceKeyFields(ownerMapping.getTargetKeyFields());
95             mapping.setSourceRelationKeyFields(ownerMapping.getTargetRelationKeyFields());
96                 
97             // Add all the target foreign keys we found on the owner.
98
mapping.setTargetKeyFields(ownerMapping.getSourceKeyFields());
99             mapping.setTargetRelationKeyFields(ownerMapping.getSourceRelationKeyFields());
100         }
101
102         // Add the mapping to the descriptor.
103
m_descriptor.addMapping(mapping);
104     }
105 }
106
Popular Tags