| Summary: | The method addMapping() of DynamicTypeBuilder should have public visibility | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Product: | z_Archived | Reporter: | Rohit Banga <rohit.banga> | ||||||
| Component: | Eclipselink | Assignee: | Nobody - feel free to take it <nobody> | ||||||
| Status: | RESOLVED FIXED | QA Contact: | |||||||
| Severity: | normal | ||||||||
| Priority: | P3 | CC: | christopher.delahunt, david.minsky, mohamad.raja, rohit.banga, tom.ware | ||||||
| Version: | unspecified | ||||||||
| Target Milestone: | --- | ||||||||
| Hardware: | All | ||||||||
| OS: | All | ||||||||
| Whiteboard: | submitted_patch simple_fix | ||||||||
| Attachments: |
|
||||||||
|
Description
Rohit Banga
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 |