Implementing a many-to-many relationship between entity types
To add a many-to-many relationship between entity types to the data model, you need to do the following:
- First, create a separate entity with a
typeattribute ofversionableor a child ofversionable. - Add non-nullable foreign keys to each end of the many-to-many relationship.
- Add a unique index on each of the foreign keys.
These steps create a classic join entity.
The following example illustrates how to create a many-to-many relationship between Account and Contact entity types.
- It first creates a versionable entity,
AccountContact, by setting thetypeattribute toretireable. - It then defines foreign keys to
AccountandContact. - Finally, it adds indexes to these foreign keys.
The code looks similar to the following:
<entity xmlns="http://guidewire.com/datamodel"
entity="AccountContact"
table="accountcontact"
type="retireable"
desc="Join entity modeling many-to-many relationship between Account and Contact entities">
<foreignkey columnName="AccountID"
fkentity="Account"
name="Account"
nullok="false"/>
<foreignkey columnName="ContactID"
fkentity="Contact"
name="Contact"
nullok="false"/>
<index name="accountcontacts" unique="true">
<indexcol keyposition="1" name="AccountID"/>
<indexcol keyposition="2" name="ContactID"/>
</index>
</entity>
To access the relationship, you need to add an array of the new join entities to either end or to both ends of the relationship. For example:
<extension xmlns="http://guidewire.com/datamodel" entityName="Account">
<array arrayentity="AccountContact"
desc="All the AccountContact entities related to Account."
name="AccountContacts"/>
</extension>
This provides an array of AccountContact entities on Account.
