A bidirectional OneToOne association with a shared primary key

#1

Hi,

In Hibernate, it is possible (using Hibernate specific annotations) to define a bidirectional @OneToOne association that has a shared primary key, example:

http://fruzenshtein.com/bidirectional-one-to-one-primary-key-association/

Can ObjectDB support this same specific relationship, and if so, could you provide or point me to a concrete code example that at least details all of the required annotations on the respective Entities and any special initialization calls that need to be made ? Also, if this is possible in ObjectDb, is it more efficient to use a join column or a join table - and for whichever is more efficient, could that be the example you provide ?

Thanks

 

#2

ObjectDB manages relationships automatically and silently ignores mapping annotations.

You can have a bidirectional one to one association, but it cannot be based on identical primary keys, i.e. the primary key will be stored at least on one side of the relationship, even if it is identical to the primary key (causing duplication). Accordingly, you must set the reference, at least at the owner side, otherwise the association will not be created.

Regarding performance, in many cases managing two unidirectional relationships is more efficient than one bidirectional relationship, because accessing the owner side from the mapped side in a bidirectional relationship requires running a query. If you do use a one to one bidirectional relationship, the owner side should be side from which traversing is more common, and you may want to set an index on the field to accelerate traversing in the other direction.

ObjectDB Support
#3

Hi,

Thanks for the response. In order that I can be sure to obtain the performance advantage you mentioned, can you provide or point me to any concrete code example that at least details all of the required annotations on the respective Entities and any special initialization calls that need to be made to simulate a bidirectional relationship using "two unidirectional relationships" which (I assume) would not include a mappedBy attribute ?

Thanks

#4

No annotations are required by ObjectDB (you may use @OneToOne annotations with no mappedBy):

@Entity
public class Entity1 {
    Entity2 e2;
}

@Entity
public class Entity2 {
    Entity1 e1;
}

but your application will have to keep these two unidirectional relationships synchronized.

 

 

ObjectDB Support

Reply