Posted by Giampiero Granatella
on June 4, 2009
In this tutorial we'll learn the various types of relationship that can connect classes. To do so, we are going to develop a simple application: a movie database.
First we'll develop a simple model with four classes, then we'll introduce the various relationships one by one.
- 1 The data model: just the classes
- 2 Many-To-One relationships
- 3 Many-To-Many Relationships
- 4 One-To-One Relationships
As usual, let's go "upstairs" and start modeling! Our application has the following classes with the specified attributes (if you don't know how to create a class see tutorial 2):
- Movie, with the attributes: title (text, required, in name, in details), year (integer, in summary, in details), plot (text, multiline, in details).
- Rating, with the attribute value (text, required, in name, in details)
- Soundtrack, with the attributes: name (text, required, in name, in details), authors (text, required, in summary, in details)
- Actor, with the attribute name (text, required, in name, in details)
"Downstairs", put some data for the Ratings. Create four objects and enter the following "values": poor, ok, good, must see.
Now that we have our classes, we can introduce the relationships...
In our example we want to relate movies to ratings. A movie has a rating and, at the same time, a rating can be applied to many movies: we need a many-to-one relationship between the classes Movie and Rating.
In Portofino, a many-to-one relationship is implemented through a Relationship Attribute, which specifies the two ends of the relationship:
- the "many" end of the relationship is the "class" that owns the attribute,
- the "one" end of the relationship is the "opposite end class" that is referenced by the attribute.
So in the example, Movie is the "class" while Rating is the "opposite end class".
Let's do this step by step:
- go "upstairs"
- click on the "Classes" tab
- click on "Movie" - we start from the class on the "many" side
- "Add Attribute"
- select "Relationship Attribute"
- enter the name and pretty of the relationship attribute ("rating" in this case).
In "relationship-specific information":
- Select Rating as the opposite end class.
- in "opposite end name" enter the label that will be used when looking at the relationship from the Rating side: in this case type "Movies".
- Mark the attribute "in details".
Click on "Create" to confirm the operation.
A relationship attribute is similar to any other attribute. Downstairs, during the creation, we can choose among all the objects of the class "Rating" through a drop down list.
The relationship is visible also from the "Ratings" side of the relationship. Click on the Ratings tab and select the "must see" object; you will see all the movies that have a "must see" rating (See below).
All the relationships are navigable: you can click on the hyperlinks on one side and see the related object(s) on the other side.
The one-to-many relationship is the other side of the same coin. You implicitly created a one-to-many when you created the many-to-one: they are the same thing, from two different perspectives.
Go "upstairs", in the "Rating" class you'll find this type of relationship under the "One-to-many relationships".
You can also create a "One-to-many" relationship clicking on "Add relationship attribute". The form is the same as before, but now you start from the "opposite end class" and you have to select the class that will have this rel as a relationship attribute.
In our movie database, we want to relate actors to movies: an actor can play in several movies, and a movie has several actors. This is a clear example of many-to-many relationship.
A many-to-many relationship can be implemented as two many-to-one relationships in a junction (or intersection) class.
In Portofino a junction class is a class with the "Relationship" flag selected. This class acts as a container for two relationship attributes pointing to the two opposite end classes ("Movie" and "Actor" in our example), which will be connected.
The "Relationship" flag instructs Portofino to hide the junction class downstairs, and to show directly the related objects which take part in the many-to-many rel.
Let's do this step by step.
- Go "upstairs"
- Click on the "Classes" tab, and then "Create"
- Insert "play" as name, pretty name and pretty plural, then check the "Relationship" flag
- Click on the "Create" button
Now, we can create the two relationship attributes to "Movie" and to "Actor" as we did in the previous section. Remember to check the flags "in summary" and "in the details" to ease the navigation.
We can also add a third attribute to the junction class. In our example we specify the role played by an actor in a movie. This is a simple "role" text attribute on the "play" class.
Go "downstairs" and test the application. You can add actors and movies. You'll find the "Actor" tab in movies and the "Movie" tab in actors. When you connect an actor to a movie (or viceversa) you can insert the role played.
We introduce the soundtracks in the movie database. We notice that a movie has only one soundtrack and the sound track belongs to only one movie. We'll implement that trough a one-to-one relationship.
In Portofino a one-to-one relationship is similar to a many-to-one relationship. The only difference is that you have to check the one-to-one flag during the creation. Let's do this step by step.
- go upstairs
- click on the "Classes" Tab
- click on "Soundtrack", our starting class
- click on "add relationship attribute"
- insert "name" and "pretty name"
- select "Movie" as "opposite end class"
- check the flags "one-to-one", "context" and "immutable" ("immutable" will make a relation between objects not changeable)
Go "downstairs". You'll see that we can add only one sound track to a movie.
So now that you known relationships, you can enjoy ManyDesigns Portofino... "this is an offer you can't refuse..."