KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
25
26 import javax.persistence.OneToMany;
27
28 import oracle.toplink.essentials.internal.helper.DatabaseField;
29
30 import oracle.toplink.essentials.mappings.OneToOneMapping;
31 import oracle.toplink.essentials.mappings.OneToManyMapping;
32 import oracle.toplink.essentials.mappings.ManyToManyMapping;
33
34 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor;
35 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor;
36
37 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor;
38 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject;
39
40 /**
41  * A OneToMany relationship accessor.
42  *
43  * @author Guy Pelletier
44  * @since TopLink EJB 3.0 Reference Implementation
45  */

46 public class OneToManyAccessor extends CollectionAccessor {
47     /**
48      * INTERNAL:
49      */

50     public OneToManyAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) {
51         super(accessibleObject, classAccessor);
52         
53         // Process the annotation specifics if there is one, otherwise the
54
// defaults use the defaults which are initialized in the super class.
55
OneToMany oneToMany = getAnnotation(OneToMany.class);
56         
57         if (oneToMany != null) {
58             setFetchType(oneToMany.fetch());
59             setMappedBy(oneToMany.mappedBy());
60             setCascadeTypes(oneToMany.cascade());
61             setTargetEntity(oneToMany.targetEntity());
62         }
63     }
64     
65     /**
66      * INTERNAL:
67      */

68     public boolean isOneToMany() {
69         return true;
70     }
71     
72     /**
73      * INTERNAL:
74      * Process an @OneToMany or one-to-many element into a TopLink OneToMany
75      * mapping. If a JoinTable is found however, we must create a ManyToMany
76      * mapping.
77      */

78     public void process() {
79         String JavaDoc mappedBy = getMappedBy();
80         
81         // Should be treated as a uni-directional mapping using a join table.
82
if (mappedBy.equals("")) {
83             // If we find a JoinColumn(s), then throw an exception.
84
if (hasJoinColumn() || hasJoinColumns()) {
85                 getValidator().throwUniDirectionalOneToManyHasJoinColumnSpecified(getJavaClass(), getAttributeName());
86             }
87             
88             // Create a M-M mapping and populate it from the OneToMany.
89
ManyToManyMapping mapping = new ManyToManyMapping();
90             populateCollectionMapping(mapping, m_logger.ONE_TO_MANY_MAPPING_REFERENCE_CLASS);
91             
92             // Process the @JoinTable.
93
processJoinTable(getJoinTable(), mapping);
94             
95             // Add the mapping to the descriptor.
96
m_descriptor.addMapping(mapping);
97         } else {
98             // Create a 1-M mapping and populate it from the @OneToMany.
99
OneToManyMapping mapping = new OneToManyMapping();
100             populateCollectionMapping(mapping, m_logger.ONE_TO_MANY_MAPPING_REFERENCE_CLASS);
101             
102             // Non-owning side, process the foreign keys from the owner.
103
OneToOneMapping ownerMapping = null;
104             if (getOwningMapping().isOneToOneMapping()){
105                 ownerMapping = (OneToOneMapping)getOwningMapping();
106             }else {
107                 //gf730 - If improper mapping encountered, throw an exception
108
getValidator().throwIvalidMappingEncountered(getJavaClass(), getReferenceClass());
109             }
110                 
111             Map JavaDoc<DatabaseField, DatabaseField> keys = ownerMapping.getSourceToTargetKeyFields();
112             for (DatabaseField fkField : keys.keySet()) {
113                 mapping.addTargetForeignKeyField(fkField, keys.get(fkField));
114             }
115             
116             // Add the mapping to the descriptor.
117
m_descriptor.addMapping(mapping);
118         }
119     }
120 }
121
Popular Tags