betweenGo

Specifying One-to-Many Relationship in ATG Repositories

by Frank Kim on Jan.21, 2010, under Repository

Monta driving on Flickr
(Photo: Monta driving by Yogma)

Specifying one-to-many relationships is ridiculously easy in Ruby on Rails.  Unfortunately it’s not so straight-forward in ATG repositories.

First you specify the “belongs to” relationship.  In this example the player belongs to a team.

<item-descriptor name="player">
  <table name="team_players" type="auxiliary" id-column-names="team_id" shared-table-sequence="1">
    <property name="team" column-name="team_id" item-type="team" />
  </table>
</item-descriptor>

Then you specify the “has many” relationship.  In this example the team has many players.

<item-descriptor name="team">
  <table name="team_players" type="multi" id-column-names="player_id" shared-table-sequence="2">
    <property name="players" column-name="player_id" data-type="set" component-item-type="player" />
  </table>
</item-descriptor>

Note the trick is specifying the “shared-table-sequence.”

Here is the SQL for the table that specifies this relationship in our example.

CREATE TABLE team_players
(
  team_id     VARCHAR2(40)  NOT NULL,
  player_id   VARCHAR2(40)  NOT NULL,
  CONSTRAINT team_players_pk PRIMARY KEY (team_id, player_id),
  CONSTRAINT team_players_players_fk foreign key (player_id) references players (id),
  CONSTRAINT team_players_team_fk foreign key (team_id) references teams (id)
);


  • Share/Bookmark

Related posts:

  1. user-defined property type gotcha’s
  2. Best Practices for Creating Tables
  3. ATG Product Bundles
  4. How to Alter Table
  5. ATG Repository User-Defined Property Types

:,

2 Comments for this entry

  • pavithra

    column-name and id-column-names should interchange ?

  • Frank Kim

    hi pavithra,
    thanks for your comment.
    i’m not sure what you mean though. are you saying that the column-name and id-column-names should be switched in my code example? i believe it’s correct.
    thanks,
    frank

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!