This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
View | Details | Raw Unified | Return to bug 266912 | Differences between
and this patch

Collapse All | Expand All

(-)jpa/eclipselink.jpa.test/src/org/eclipse/persistence/testing/tests/jpa/metamodel/MetamodelMetamodelTest.java (-22 / +18 lines)
Lines 3222-3229 Link Here
3222
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
3222
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
3223
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
3223
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
3224
                Type keyType = anAttribute.getKeyType(); 
3224
                Type keyType = anAttribute.getKeyType(); 
3225
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
3225
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
3226
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
3226
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
3227
                assertNotNull(keyType);
3227
                assertNotNull(keyType);
3228
                assertTrue(keyType instanceof Type);
3228
                assertTrue(keyType instanceof Type);
3229
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
3229
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 3290-3297 Link Here
3290
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
3290
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
3291
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
3291
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
3292
                Type keyType = anAttribute.getKeyType(); 
3292
                Type keyType = anAttribute.getKeyType(); 
3293
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
3293
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
3294
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
3294
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
3295
                assertNotNull(keyType);
3295
                assertNotNull(keyType);
3296
                assertTrue(keyType instanceof Type);
3296
                assertTrue(keyType instanceof Type);
3297
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());                
3297
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());                
Lines 4241-4248 Link Here
4241
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
4241
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
4242
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
4242
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
4243
                Type keyType = anAttribute.getKeyType(); 
4243
                Type keyType = anAttribute.getKeyType(); 
4244
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4244
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4245
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4245
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4246
                assertNotNull(keyType);
4246
                assertNotNull(keyType);
4247
                assertTrue(keyType instanceof Type);
4247
                assertTrue(keyType instanceof Type);
4248
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4248
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4283-4290 Link Here
4283
                //@OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
4283
                //@OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
4284
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
4284
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
4285
                Type keyType = anAttribute.getKeyType(); 
4285
                Type keyType = anAttribute.getKeyType(); 
4286
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4286
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4287
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4287
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4288
                assertNotNull(keyType);
4288
                assertNotNull(keyType);
4289
                assertTrue(keyType instanceof Type);
4289
                assertTrue(keyType instanceof Type);
4290
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4290
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4412-4419 Link Here
4412
                // Same as UC1a - that is missing the @MapKey
4412
                // Same as UC1a - that is missing the @MapKey
4413
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC7;
4413
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC7;
4414
                Type keyType = anAttribute.getKeyType(); 
4414
                Type keyType = anAttribute.getKeyType(); 
4415
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4415
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4416
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4416
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4417
                assertNotNull(keyType);
4417
                assertNotNull(keyType);
4418
                assertTrue(keyType instanceof Type);
4418
                assertTrue(keyType instanceof Type);
4419
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4419
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4455-4462 Link Here
4455
                // Same as UC1a - that is missing the @MapKey name attribute
4455
                // Same as UC1a - that is missing the @MapKey name attribute
4456
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC8;
4456
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC8;
4457
                Type keyType = anAttribute.getKeyType(); 
4457
                Type keyType = anAttribute.getKeyType(); 
4458
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4458
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4459
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present or missing name attribute - we default to the PK
4459
                assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4460
                assertNotNull(keyType);
4460
                assertNotNull(keyType);
4461
                assertTrue(keyType instanceof Type);
4461
                assertTrue(keyType instanceof Type);
4462
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4462
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4542-4549 Link Here
4542
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
4542
                // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
4543
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
4543
                // Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
4544
                Type keyType = anAttribute.getKeyType(); 
4544
                Type keyType = anAttribute.getKeyType(); 
4545
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4545
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4546
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4546
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4547
                assertNotNull(keyType);
4547
                assertNotNull(keyType);
4548
                assertTrue(keyType instanceof Type);
4548
                assertTrue(keyType instanceof Type);
4549
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4549
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4585-4592 Link Here
4585
                //@OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
4585
                //@OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
4586
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
4586
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
4587
                Type keyType = anAttribute.getKeyType(); 
4587
                Type keyType = anAttribute.getKeyType(); 
4588
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4588
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4589
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4589
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4590
                assertNotNull(keyType);
4590
                assertNotNull(keyType);
4591
                assertTrue(keyType instanceof Type);
4591
                assertTrue(keyType instanceof Type);
4592
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4592
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
Lines 4674-4681 Link Here
4674
                assertNotNull(keyType);
4674
                assertNotNull(keyType);
4675
                assertTrue(keyType instanceof Type);
4675
                assertTrue(keyType instanceof Type);
4676
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4676
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4677
                
4678
                
4679
            } catch (IllegalArgumentException iae) {
4677
            } catch (IllegalArgumentException iae) {
4680
                //iae.printStackTrace();
4678
                //iae.printStackTrace();
4681
                exceptionThrown = true;
4679
                exceptionThrown = true;
Lines 4713-4725 Link Here
4713
                // Same as UC1a - that is missing the @MapKey
4711
                // Same as UC1a - that is missing the @MapKey
4714
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC7;
4712
                //private Map<String, HardwareDesigner> hardwareDesignersMapUC7;
4715
                Type keyType = anAttribute.getKeyType(); 
4713
                Type keyType = anAttribute.getKeyType(); 
4716
                //assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present
4714
                assertEquals(String.class, keyJavaType); // When @MapKey(name="name") is present - or generics are set
4717
                assertEquals(Integer.class, keyJavaType); // When @MapKey is not present - we default to the PK
4715
                //assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
4718
                assertNotNull(keyType);
4716
                assertNotNull(keyType);
4719
                assertTrue(keyType instanceof Type);
4717
                assertTrue(keyType instanceof Type);
4720
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4718
                assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
4721
                
4722
                
4723
            } catch (IllegalArgumentException iae) {
4719
            } catch (IllegalArgumentException iae) {
4724
                //iae.printStackTrace();
4720
                //iae.printStackTrace();
4725
                exceptionThrown = true;
4721
                exceptionThrown = true;
(-)jpa/org.eclipse.persistence.jpa/src/org/eclipse/persistence/internal/jpa/metamodel/MapAttributeImpl.java (-13 / +82 lines)
Lines 66-113 Link Here
66
66
67
    /**
67
    /**
68
     * INTERNAL:
68
     * INTERNAL:
69
     * @param managedType
69
     * Create a new MapAttribute instance.
70
     * @param mapping
70
     * The elementType field is instantiated in the superclass.
71
     * @param validationEnabled
71
     * The keyType field is instantiated in this constructor by using one of the following
72
     * A) MapContainerPolicy by consulting the keyField or PK class
73
     * B) MappedKeyMapContainerPolicy by using the mapKeyTargetType on the keyMapping or the attributeClassification on the attributeAccessor 
74
     * @param managedType - the owning type
75
     * @param mapping - contains the mapping policy
76
     * @param validationEnabled - report errors in the metamodel
72
     */
77
     */
73
    protected MapAttributeImpl(ManagedTypeImpl<X> managedType, CollectionMapping mapping, boolean validationEnabled) {
78
    protected MapAttributeImpl(ManagedTypeImpl<X> managedType, CollectionMapping mapping, boolean validationEnabled) {
74
        // Set the managedType (X or owning Type)
79
        // Set the managedType (X or owning Type)
75
        super(managedType, mapping, validationEnabled);
80
        super(managedType, mapping, validationEnabled);
76
        // Override the elementType (V or Map value)
77
        // We need to set the keyType Type that represents the type of the Map key for this mapping
81
        // We need to set the keyType Type that represents the type of the Map key for this mapping
78
        ContainerPolicy policy = mapping.getContainerPolicy();
82
        ContainerPolicy policy = mapping.getContainerPolicy();
79
        ClassDescriptor policyElementDescriptor = policy.getElementDescriptor();
80
        Object policyKeyType = policy.getKeyType(); // returns a Class<?> or descriptor (via AggregateObjectMapping)        
81
        Type<?> aKeyType = null;
83
        Type<?> aKeyType = null;
82
        // Default to Object class for any variant cases that are not handled
83
        Class<?> javaClass = null;
84
        Class<?> javaClass = null;
85
        MapKeyMapping keyMapping = null; 
86
        ClassDescriptor policyElementDescriptor = policy.getElementDescriptor();
87
        Object policyKeyType = null;
88
  
89
        /**
90
         * We have the following policy structure and behavior
91
         * MapContainerPolicy (use keyField or PK class)
92
         *   --> MappedKeyMapContainerPolicy (use keyMapping.mapKeyTargetType or attributeClassification) 
93
         *   
94
         *   Use Case segmentation for keyType 
95
                A) MapContainerPolicy 
96
                    A1) keyField set (lazy loaded) 
97
                        UC2 
98
                            @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC2") 
99
                            @MapKey(name="name") 
100
                            private Map<String, HardwareDesigner> hardwareDesignersMapUC2; 
101
                        UC4 
102
                            @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC4") 
103
                            @MapKey(name="name") 
104
                            private Map hardwareDesignersMapUC4; 
105
                        UC8 
106
                            @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC8") 
107
                            @MapKey // name attribute will default to "id" 
108
                            private Map<Integer, HardwareDesigner> hardwareDesignersMapUC8; 
109
                    A2) Use mapPolicy.elementDescriptor.cmppolicy.pkClass (since KeyField == null) 
110
                        Computer.enclosures 
111
                            @OneToMany(mappedBy="computer", cascade=ALL, fetch=EAGER) 
112
                            @MapKey // key defaults to an instance of the composite pk class 
113
                            private Map<EnclosureIdClassPK, Enclosure> enclosures; 
114
                        or (get keyClass from mapping if the Id is a get() function) 
115
                            TBD - use reflection 
116
                B) MappedKeyMapContainerPolicy 
117
                    B1) mapKeyTargetType set on the keyMapping 
118
                        UC9 
119
                            @OneToMany(cascade=CascadeType.ALL, mappedBy="mappedManufacturerUC9") 
120
                            private Map<Board, Enclosure> enclosureByBoardMapUC9; 
121
                    B2) Use AttributeClassification (since keyMapping.attributeAccessor.attributeClass == null) 
122
                        UC1a - used to return the Integer PK 
123
                            @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a") 
124
                            private Map<String, HardwareDesigner> hardwareDesignersMapUC1a; 
125
                        UC7 - used to return the Integer PK 
126
                            @OneToMany(targetEntity=HardwareDesigner.class, cascade=ALL, mappedBy="mappedEmployerUC7") 
127
                            private Map<String, HardwareDesigner> hardwareDesignersMapUC7; 
128
         */
129
        
130
        // Step 1: We check the Map policy for the keyMapping
131
        if(policy.isMapPolicy()) { // There is always a MapPolicy
132
            // check for Either a generic Map (MapContainerPolicy) or specific MappedKeyMapContainerPolicy subclass
133
            if(policy.isMappedKeyMapPolicy()) {
134
                // The cast below is unavoidable because getKeyMapping() is not overridden from the MapContainerPolicy superclass
135
                keyMapping = ((MappedKeyMapContainerPolicy)policy).getKeyMapping();
136
                policyKeyType = keyMapping.getMapKeyTargetType();
137
                // DirectToFieldMapping implements MapKeyMapping - here we get the key from the attributeClassification
138
                if(null == policyKeyType) {
139
                    policyKeyType = ((DatabaseMapping)keyMapping).getAttributeClassification();
140
                }
141
            } else {
142
                // Assume we have a MapContainerPolicy general superclass with a lazy-loaded keyType
143
                policyKeyType = policy.getKeyType(); // returns a Class<?> or descriptor (via AggregateObjectMapping)
144
                // The keyType will be null on the MapContainerPolicy when the keyField is null
145
            }
146
        }
147
        
148
        // Step 2: We determine the java class from the policyKeyType (class or ClassDecriptor) - and perform alternate keyType lookup
84
        if(null == policyKeyType) {
149
        if(null == policyKeyType) {
85
            // No policy key type = IdClass (use CMP3Policy.pkClass)
150
            // The keyType will be null on a MapContainerPolicy when the keyField is null - usually a composite key
86
            if(managedType.isIdentifiableType()) {
151
            if(managedType.isIdentifiableType()) {
87
                // Use the CMPPolicy on the element not the one on the managedType
152
                // Use the PK of the element not the one on the managedType
88
                if(policyElementDescriptor != null && 
153
                // Case: @MapKey private Map<K,V> // no name column specified
89
                        policyElementDescriptor.getCMPPolicy() != null) {
154
                if(policyElementDescriptor != null && policyElementDescriptor.getCMPPolicy() != null) {
90
                    javaClass = policy.getElementDescriptor().getCMPPolicy().getPKClass();
155
                    javaClass = policy.getElementDescriptor().getCMPPolicy().getPKClass();
91
                }
156
                }
157
                // Case: @MapKey private Map<K,V> // no name column specified and the PK is defined by a method
92
                if(null == javaClass) {
158
                if(null == javaClass) {
93
                    // check for a @MapKeyClass annotation
94
                    if(policy.isMappedKeyMapPolicy()) {                            
159
                    if(policy.isMappedKeyMapPolicy()) {                            
95
                        javaClass = getOwningPKTypeWhenMapKeyAnnotationMissingOrDefaulted(
160
                        javaClass = getOwningPKTypeWhenMapKeyAnnotationMissingOrDefaulted(
96
                                (MappedKeyMapContainerPolicy)policy);
161
                                (MappedKeyMapContainerPolicy)policy);
97
                    }
162
                    }
98
                }
163
                }
99
            }
164
            }
100
        } else {            
165
        } else {
166
            // Process the policyKeyType
101
            if(policyKeyType instanceof ClassDescriptor) { // from AggregateObjectMapping
167
            if(policyKeyType instanceof ClassDescriptor) { // from AggregateObjectMapping
102
                javaClass = ((ClassDescriptor)policyKeyType).getJavaClass();
168
                javaClass = ((ClassDescriptor)policyKeyType).getJavaClass();
103
            } else {
169
            } else {
104
                javaClass = (Class<?>)policyKeyType;
170
                javaClass = (Class<?>)policyKeyType;
105
            }            
171
            }            
106
        }
172
        }
173
 
174
        // Optional: catch any holes in our keyType logic (8 hits in clover coverage)
107
        if(null == javaClass) {
175
        if(null == javaClass) {
108
            javaClass = Object.class;
176
            javaClass = Object.class;
109
        }                                
177
        }                                
110
        
178
        
179
        // Step 3: We wrap the java type in a Metamodel Type instance or retrieve an existing Type
111
        aKeyType = getMetamodel().getType(javaClass);
180
        aKeyType = getMetamodel().getType(javaClass);
112
        this.keyType = (Type<K>) aKeyType;
181
        this.keyType = (Type<K>) aKeyType;
113
    }
182
    }

Return to bug 266912