Community
Participate
Working Groups
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.
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.
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?
Checked into trunk (2.3) - Revision: 9136
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
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.
Thanks Chris. That solves it! I didn't see the addXX() method. Was looking for the setXX() method.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink