Bidirectional one-to-one relations with dynamic entities [message #1111425] |
Wed, 18 September 2013 03:44  |
Eclipse User |
|
|
|
Hi,
In our project we're using eclipse link dynamic entities. These are defined using a DynamicTypeBuilder.
When using 'normal' entities you can create a bidirectional one-to-one relation using @OneToOne on one end of the association and @MappedBy on the other end.
What is the equivalent of this using the DynamicTypeBuilder ? It does provide a addOneToOneMapping(...) method, but I can't really find the equivalent of the @MappedTo.
An example would be really helpful.
Thanks,
Xander
|
|
|
Re: Bidirectional one-to-one relations with dynamic entities [message #1111702 is a reply to message #1111425] |
Wed, 18 September 2013 11:51  |
Eclipse User |
|
|
|
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 11:52] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04013 seconds