Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Bi-Directional relationships in Jazz repository

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

0 votes



5 answers

Permanent link
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.

0 votes


Permanent link
Could he somehow use Links to have the effect of a bi-directional relationship?

0 votes


Permanent link
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.

0 votes


Permanent link
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.

0 votes


Permanent link
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

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,941

Question asked: Jul 06 '07, 10:55 a.m.

Question was seen: 9,265 times

Last updated: Jul 06 '07, 10:55 a.m.

Confirmation Cancel Confirm