Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Bidirectional one-to-one relations with dynamic entities
Bidirectional one-to-one relations with dynamic entities [message #1111425] Wed, 18 September 2013 03:44 Go to next message
Xander Uiterlinden is currently offline Xander Uiterlinden
Messages: 6
Registered: July 2009
Junior Member
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 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1017
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 11:52]

Report message to a moderator

Previous Topic:Cascade.remove performance suggestions
Next Topic:Implementing XMLNameTransformer breaks JSON mapping of abstract classes
Goto Forum:
  


Current Time: Thu Aug 21 20:08:33 EDT 2014

Powered by FUDForum. Page generated in 0.01601 seconds