Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 339529 - The method addMapping() of DynamicTypeBuilder should have public visibility
Summary: The method addMapping() of DynamicTypeBuilder should have public visibility
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard: submitted_patch simple_fix
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-10 09:37 EST by Rohit Banga CLA
Modified: 2022-06-09 10:29 EDT (History)
5 users (show)

See Also:


Attachments
Code that implements the usecase mentioned in the eclipselink-users message referenced in the bug (10.21 KB, text/java)
2011-03-10 09:37 EST, Rohit Banga CLA
no flags Details
Proposed Fix of changing method visibility from protected to public (901 bytes, patch)
2011-03-10 13:32 EST, Rohit Banga CLA
david.minsky: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Rohit Banga CLA 2011-03-10 09:37:02 EST
Created attachment 190862 [details]
Code that implements the usecase mentioned in the eclipselink-users message referenced in the bug

In order to configure mappings on DynamicTypeBuilder we can either use the wrapper methods addOneToOneMapping(), addOneToManyMapping() etc. or we can use the DynamicTypeBuilder.addMapping(DatabaseMapping) method.

The second option allows some additional configuration options to be configured on the mapping.

For Example - the method addOneToOneMapping() expects the foreign key to be on the source side of the table.

However in a scenario like that mentioned in:
http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg05979.html

One may need to configure a mapping in the reverse direction for which targetForeignKeyField needs to be configured on the Mapping.

To support this it seems the method DynamicTypeBuilder.addMapping(mapping) should be made public from protected.
Comment 1 Rohit Banga CLA 2011-03-10 12:00:00 EST
I tried the following workaround:

created my own method with the contents of
DynamicTypeBuilder.addMapping(mapping) and ran the program. It allows me to add OneToOneMapping with the targetForeignKey specified.
Comment 2 Rohit Banga CLA 2011-03-10 13:32:17 EST
Created attachment 190895 [details]
Proposed Fix of changing method visibility from protected to public

While using dynamic JPA, the creation of mappings having targetForeignKeys the wrapper methods like addOneToOneMapping() are not sufficient. As proposed in the bug description, the visibility of method DynamicTypeBuilder.addMapping(DatabaseMapping) should be changed from protected to public to support these scenarios.

Could you please review this bug fix for inclusion?
Comment 3 David Minsky CLA 2011-03-16 14:46:51 EDT
Checked into trunk (2.3) - Revision: 9136
Comment 4 Rohit Banga CLA 2011-03-27 02:16:11 EDT
Thanks for merging the fix.
One related question:

To add the targetForeignKeyField I use the method

OneToOneMapping.setTargetForeignKeyFieldName(targetForeignKeyFieldName);

For composite primary keys, however, the corresponding Foreign Keys are also composite.
Is there an API to configure composite foreign keys?

Not sure if it is OK to use 
OneToOneMapping.setTargetToSourceKeyFields()? It is marked as internal and javadoc for this method isn't very clear.

Thanks
Comment 5 Chris Delahunt CLA 2011-03-28 12:34:27 EDT
Confusion here is because the helper method on DynamicTypeBuilder takes a list of strings representing the foreign keys, while OneToOneMapping.setTargetForeignKeyFieldName only takes two strings.  What is needed to create the mapping is to duplicate what the DynamicTypeBuilder  addOneToOneMapping method does, which is:

public OneToOneMapping addOneToOneMapping(String name, DynamicType refType, String... fkFieldNames) {
 if (fkFieldNames == null || refType.getDescriptor().getPrimaryKeyFields().size() != fkFieldNames.length) {
            throw new IllegalArgumentException("Invalid FK field names: " + fkFieldNames + " for target: " + refType);
        }

        OneToOneMapping mapping = new OneToOneMapping();
        mapping.setAttributeName(name);
        mapping.setReferenceClass(refType.getJavaClass());

        for (int index = 0; index < fkFieldNames.length; index++) {
            String targetField = refType.getDescriptor().getPrimaryKeyFields().get(index).getName();
            mapping.addForeignKeyFieldName(fkFieldNames[index], targetField);
        }

        return (OneToOneMapping) addMapping(mapping);
    }

In this code, to specify that the foreign key is in the target, you would create a mapping in the same way, but use setTargetToSourceKeyFields instead of addForeignKeyFieldName - but still calling it for each key/fk pair.  Nothing else would change.  

Please file an enhancement if any mapping+configuration settings are used often enough that they might justify adding a helper method to DynamicTypeBuilder.
Comment 6 Rohit Banga CLA 2011-03-28 12:39:48 EDT
Thanks Chris. That solves it!
I didn't see the addXX() method. Was looking for the setXX() method.
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:29:33 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink