It's all about the answers!

Ask a question

Bi-Directional relationships in Jazz repository


Igor Foox (41) | asked Jul 06 '07, 10:55 a.m.
Hi,

I am creating a component with a model, but I am having trouble with
bidirectional relationships between items. Basically I want to have a
one-to-many relationship between two item types where the eOpposite EMF
attribute is specified on both ends of the relationship. I can create
items like this but I'm having trouble saving them in the database.

Searching for "eOpposite" in my jazz workspace I see some references in
EMF generated files, but it seems that they have a 'dbPersistable=false'
attribute in the ecore file (at least the ones I checked). Does the
repository model support bidirectional relationship, and if yes, are
there any (simple) examples of how to use them?

Thanks,
Igor

5 answers



permanent link
Rafik Jaouani (5.0k16) | answered Jul 11 '07, 2:10 p.m.
JAZZ DEVELOPER
I don't think we support bi-directional relationships, because of how we transform the logical ecore model into an implementation that changes the reference types to be handle types. Supporting bidirectional relationships will also cause problems when saving one end and not the other.
I don't think this is a major shortcoming as all bidirectional relationships buy you is the automatic setting of the other end when one end is set. If you want to enforce the bidirectionality of a relationship, you can always wrap the saving of both ends in one service call and have both saves happen in one transaction.

permanent link
Bill Higgins (24611) | answered Jul 11 '07, 6:26 p.m.
Could he somehow use Links to have the effect of a bi-directional relationship?

permanent link
Rafik Jaouani (5.0k16) | answered Jul 11 '07, 7:42 p.m.
JAZZ DEVELOPER
He can still model the relationship using two reference features. But he must make sure to save the two items in one transaction. Here is some pseudo-code:
i1 = ...
i2 = ...
i1.setR2(i2);
i2.setR1(i1);
service.save(i1, i2);

to break the references and redirect them to some other item:
i1.setR2(null);
i2.setR1(null);
service.save(i1, i2);
i3 = ...
i1.setR2(i3);
i3.setR1(i1);
service.save(i1, i3);

Personally, I would avoid using bi-directional relationships for keeping things simple and I can always use queries and queryable references to find out the other non obvious end of a relationship.

permanent link
Muhtar Akbulut (9111) | answered Jul 11 '07, 8:41 p.m.
JAZZ DEVELOPER
Could he somehow use Links to have the effect of a bi-directional relationship?

Links component can be used. Rafik's comments are applicable to links component (for this case) as well: two save calls in one transaction to the two linked items are needed. Moreover, another call to ILinkService.saveLink() is required to save the link (which is another auditable item). Traversing the link requires ILinkService.findXXX() methods and using ILink.getSourceReference() and getTargetReference(). Not as convenient as bidirectional EMF links, but still easy enough, and avoids issues Rafik mentioned.

permanent link
Igor Foox (41) | answered Jul 16 '07, 11:25 p.m.
makbulut wrote:
Could he somehow use Links to have the effect of a bi-directional
relationship?
Links component can be used. Rafik's comments are applicable to links
component (for this case) as well: two save calls in one transaction
to the two linked items are needed. Moreover, another call to
ILinkService.saveLink() is required to save the link (which is
another auditable item). Traversing the link requires
ILinkService.findXXX() methods and using ILink.getSourceReference()
and getTargetReference(). Not as convenient as bidirectional EMF
links, but still easy enough, and avoids issues Rafik mentioned.

Thanks for all the tips! For now I'm using the simple double-save
method. But I'll look at the Links component as well, since it might
come in handy in the future.

Igor

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.