How To: Defining Many to Many Relationships
From jVantage
jVantage supports many types of relationships between entities; including one to many, one to one, and many to many. However, many to many (M2M) relationships are handled differently than all other relationship types because they are implemented using a join table in the underlying database. Although as a developer, you do not need to concern yourself with this join table (jVantage provides it automatically) it does mean that the steps involved in defining a M2M relationship are slightly different than all others. Even though implementing M2M patterns in applications is quite difficult under normal circumstances, in jVantage they are extremely simple to set up.
Unlike other relationship types, M2M's are not defined as an attribute of a field. If you select a Type Name of foreign key when defining a new field, the foreign key type drop-down menu becomes enabled, but many to many is not on the list. That's because M2M relationships are defined at the table level, even though their underlying characteristics involve fields within each of the two involved tables.
Contents |
Sample Application
As an example, let's say that we have created a very simple application with a Company entity and a Person entity, and we want to create an Employer-Employee relationship between the two that allows Companies to have more than one employee and each person to potentially have more than one employer.
To do this we have an application called M2M containing the two entities as shown below.
Company Entity
The M2M_Company entity is defined as follows:
Person Entity
Note: The attribute names of the Person entity may look strange given that there is a name, firstName and lastName field. However, this entity is used in another help document that describes how to auto populate fields using variable substitution, in which the firstName and lastName fields are used to enter data, but only the name field is displayed when the entity is viewed by setting the default value of the name field to ${firstName} ${lastName}. For this to work, all access modifiers for the field (the name field, in this case) must be turned off as follows.
The M2M_Person entity is as follows:
Defining the Relationship
To define a many to many relationship, navigate to the table that you would like to be on the left-hand side of the relationship. Even though many to many relationships are bidirectional by default, it is common to refer to one side as the left-hand side and the other as the right-hand side; lhs and rhs respectively.
We are going to define the company as the left-hand side entity and the person as a right-hand side entity. Many to many relationships are defined from the left-hand side perspective. This means that we must go to the company entity definition and select the M2 M. relationships option from the menu at left.
Click on the New Relationship option and define the relationship as follows. After defining and submitting the relationship be sure to apply your changes by selecting the apply changes button in the upper right corner.
Try It Out
Now that the relationship is defined, you can immediately go into your application to see the relationship in action. In our sample application, we've created two companies:
- Acme Plastic Rhino Factory
- Maker of fine plastic rhinos.
- Acme Sticker Company
- Maker of stickers of all shapes and sizes.
...and three potential employees:
- Andrew Jackson
- George Washington
- Ronald Reagan
If you navigate to a company, such as the Acme Sticker Company and choose the Show Employees menu option, you'll see a solitary Add button in the body of the page.
We can then choose our employees.
If you continue to experiment with the application, you'll see that the many to many relationships are implemented in a very intuitive manner. If you click on an employee after associating him or her to an employer, you'll see that the relationship is implemented in both directions.









