3

What is the effect of setting the association end ownership from "association" to "classifier" on the code that gets generated?

In other words,

0) I start with an empty diagram

1) I create class A and class B

2) I connect A and B with an association A-B

3) the association end at A is named as a

4) the association end at B is named as b

5) the association end ownership for a is set to class B (by default it is set to the association A-B itself)

6) I generate Java code for this diagram

How and why does (should) the generated code now differ if I omit step 5 ? I.e. if I use the default value ?

What is the meaning of setting the association end ownership of a to class B ? What difference does it make ?

In visual paradigm the diagrams differ as follows (second diagram shows if step 5 is omitted) :

enter image description here

enter image description here

EDIT:

In VP-UML, the generated code in both cases is the same :

public class A {

    B b;

}

public class B {

    A a;

}

So in this sense the ownership does not matter ? This is a little confusing.

jhegedus
  • 20,244
  • 16
  • 99
  • 167

4 Answers4

2

I could give you a general approach about how a general UML 2 source code generator might work (UML spec doesn't specify how to generate code from UML models)

From my point of view, if the association ends (property) belongs to the UML class, then a field should be generated in the source code class, whereas if it belongs to the association no field is generated (allows you to model unidirectional or bidirectional associations between classes). Have a look to the example I've recently posted

Community
  • 1
  • 1
ASBH
  • 541
  • 2
  • 6
  • In the title I mentioned visual paradigm as a tool example. – jhegedus Sep 23 '13 at 07:33
  • Sorry. I overlooked that. I'll adjust my answer. Did you try it, anyway ? – ASBH Sep 23 '13 at 11:19
  • The only difference - I see - is in the diagram. In the second case there has been a property (a) explicitly generated for class B. – jhegedus Sep 24 '13 at 03:35
  • Now, I don't follow. In the beginning you said (and you asked why) the generated code differs. In the edit you say the generated code is the same. Am I missing anything ? – ASBH Sep 24 '13 at 11:23
  • In VP-UML the generated code is the same, why ? What is the point then having different association ownership settings? I am trying to understand what association ownership means. – jhegedus Sep 27 '13 at 06:35
1

Ownership means that class or its instance has not only reachable way to instance of another class, but has it as an attribute (direct reference or simply contains it). Ownership does NOT belong to strict features of UML.

  • Before other things you should understand what the association A to B is.
    • Basically it is a solid line between A and B. It can represent one structure that connects class/instanc(es) of A with the class/instances of B. The structure can be of any sort and belong anywhere. All information, written about the line, describes this structure.
    • If there are two structures, one structure, that connects one instance of A with instance(s) of B and another structure that connects instance of B with instance(s) of A, you can show them both in ONE association. Then, information written about its B end describes the first structure (b->a) and info about the other end describes the other structure.
    • If you'll have more than one structure guiding from A to B, you have to draw two different associations.
    • If a joining structure is complex, you could represent it as an Association Class. There you can define more details.
    • A joining structure can connect more than two classes, then it will be shown as a large diamond with solid branches to these classes. It is still association! Attention: these two more complex associations are very badly supported by existing tools. You can easily create something absolutely senseless with them. And they are difficult. Use carefully.

example Class diagram


In C++ instance A can have the B instance not by pointer, but directly. There is NO special UML sign for it, it should be shown in the same way as normal, pointer attribute.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • this answer gives lots of information, but not about ownership. Could you please add some info specifically for ownership? – grackkle Jan 22 '15 at 17:48
  • 1
    Ownership means that class or its instance has not only reachable way to instance of another class, but has it as an attribute (direct reference or simply *contains* it). Ownership does NOT belong to strict features of UML. Thank you for your comment, my texts often have such problems. – Gangnus Jan 24 '15 at 14:38
  • @Text Problems: In that case, a second pair of eyes usually helps ;) Glad I could help! – grackkle Jan 24 '15 at 23:15
  • Additional question: why is it forbidden to have ownership on both sides? wouldn't this simply mean, that both related classes have references to each other? – grackkle Jan 24 '15 at 23:17
  • It is not forbidden! One association line can show two associations: A->B and B->A, as A-B (in older times it was A<->B). A has its association and B - its own one. And both can OWN them... It is shared association that is forbidden to have on both side - without any particular reason. – Gangnus Jan 25 '15 at 16:41
  • oh, that's an interesting aspect. I have to think about that and play around with it for a bit. Thanks again! – grackkle Jan 25 '15 at 18:04
  • 1
    Since I also struggled with what association end ownership really means, I came across this question - and during my search also found an OMG document which I think explains the matter quite nicely: [Getting it Right on the Dot](https://www.omg.org/ocup-2/documents/getting_it_right_on_the_dot.pdf). – jechterhoff Jun 25 '19 at 13:25
0

I think the convention you are trying to deal with is now deprecated.

Navigability notation was often used in the past according to an informal convention, whereby non-navigable ends were assumed to be owned by the association whereas navigable ends were assumed to be owned by the classifier at the opposite end. This convention is now deprecated.

Source: www.site.uottawa.ca

Dami
  • 677
  • 6
  • 12
  • The latest standard of UML (2.5.1) defined navigability and owenership of association ends. It is not deprecated. – LudvigH Nov 17 '22 at 15:23
0

You are expecting to see impact of ownership on navigability while ownership and navigability are orthogonal concepts.

Setting the association end ownership specifies that the association end is a feature of the class on the opposite side, which will have three impacts:

  1. in term of semantic, the definition of the owner Class will refer to the owned end and its classifier. Inversely, the definition of the Class on owned end side should not refer to the owner Class. In your example, the definition of B should refer the role a of A while the definition of A should not talk about B at all.

  2. in term of implementation, a modification of the association will only impact the object of the Owner class. In your example, let's suppose B_1 is an instance of B, and A_1 is an instance of A. Let's also suppose the cardinality of you {a,b} relationship is 1-1 and navigability is bi-directional so that we have a property B.a:A but also a property A.b:B to navigate easily from B to A and from A to B. In this case, a modification of B_1.a will be considered an update of object B_1 (an B_Updated event can be emitted to listeners) while a modification of A_1.b will NOT be considered an update of object A_1. Conceptually, no features of A_1 was updated. When changing A_1.b it is actually the associated B object that is updated.

Note: Of course, this example is a bit fictive as normally A_1.b should be read-only and derived from instances of B. A_1.b should return the instance B_i of B where B_i.a == A_1. This bring me to the third impact of ownership:

  1. when the two Classes of an association are part of two different DB schemas, it is the DB schema of the Owner Class that is the master source of the association. In your example, if A is in schema_A and B in schema_B, the relationship is mastered in schema_B. If the {a,b} relationship is a many-to-one relationship, i.e. B.b is a collection of A, we will therefore not store the FK of B in schema_A (as it would be done naturally in DB design) but we will create a specific table in schema_B to store all {a,b} relationships. Doing so will ensure that integrity of B, including the B.a feature, will be managed centrally in schema_B.

Hope it clarifies a bit the idea.