Last week I blogged about ATG unordered one-to-many relationships. Ordered one-to-many is quite similar but a little more complex.
Here again is the same example of players belonging to a team except now the players are a list property of the team.
<item-descriptor name="team"> <table name="team" type="primary" id-column-names="id"> <property name="name" required="true" /> </table> <table name="player" type="multi" multi-column-name=”idx” id-column-names="team_id"> <property name="players" column-name="id" data-type="list" component-item-type="player" /> </table> </item-descriptor> <item-descriptor name="player"> <table name="player" type="primary" id-column-names="id"> <property name="name" required="true" /> <property name="team" column-name="team_id" item-type="team" /> </table> </item-descriptor>
And here is the SQL.
CREATE TABLE team ( id VARCHAR2(40) NOT NULL, name VARCHAR2(3) NOT NULL, CONSTRAINT team_p PRIMARY KEY (id) ); CREATE TABLE player ( id VARCHAR2(40) NOT NULL, idx NUMBER(2,0),
name VARCHAR2(40), team_id VARCHAR2(40), CONSTRAINT player_p PRIMARY KEY (id) );
Now comes the tricky part. You can never just set the team in the player repository item.
This is because when you do this the idx column in the player table is not set. We could create a repository property and try to set it separately but that is bad practice. You would have to know how many players are already on the team and then set the idx to that number.
What you should do is not allow the team property to be written.
<property name="team" column-name="team_id" item-type="team" writable="false" />
And instead add the player to the team repository item.
Set<RepositoryItem> players = (Set<RepositoryItem>) team.getPropertyValue("players"); players.add(player);
Because of this requirement you cannot make the team property required for the player as discussed before.
Again if you have any questions please leave them in the comments.