|
Re: Bidirectional one-to-one relations with dynamic entities [message #1111702 is a reply to message #1111425] |
Wed, 18 September 2013 15:51 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
The mappedby causes the annotation processor to create a regular OneToOne mapping, pulling the relationship from the other side, reversing the source and foreign keys. Since the target is in the current table, it isn't considered writable from this mapping. Unfortunately the DynamicTypeBuilder does not have a convenient method to do the same, so you would have to create your own OneToOneMapping instance and pass it to the addMapping method, similar to how the addOneToOneMapping works. Something like:
OneToOneMapping mapping = new OneToOneMapping();
mapping.setAttributeName(name);
mapping.setReferenceClass(refType.getJavaClass());
mapping.addTargetForeignKeyFieldName(targetsFKFieldName, sourcesPKFieldName);
addMapping(mapping);
If there is a composite pk, you will need to add a foreign key for each field. The addOneToOneMapping takes in the current type's foreign key fields and looks up the target's primary key field values, but you might be able to get both target and source fields from the mapping in the other side (if you have a mapped by name, and that mapping was already created) using:
Map<DatabaseField, DatabaseField> map = ((OneToOneMapping)refType.getDescriptor().getMappingForAttributeName(mappedByName)).getSourceToTargetKeyFields();
for (DatabaseField fk : map.keySet()) {
mapping.addTargetForeignKeyField(fk, (DatabaseField)map.get(fk));
}
Best Regards,
Chris
[Updated on: Wed, 18 September 2013 15:52] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03120 seconds