Mapping

Monday, 28 January 2002
How does one map a relationship (= association) from an ERD to variables (= properties, fields) in a Java class?

The mapping is somewhat similar to the optimisation steps in physical database design. There are some differences, though.

  • RAM memory is fast. Performance is far less of a problem than in a database.
  • The objects in memory are usually just a subset of the information in the database.
  • RAM memory is still more scarce than disk space, although one megabyte can hold quite a few objects.
  • One-to-many relationships can be implemented in different ways: not at all, in one or two directions.
For that last point some rules of thumb: An ERD with four entities: Area, Reader, Newspaper and Subscription

to-one

This can be a one-to-one or a many-to-one relationship.
    Examples
  • From subscription to newspaper
  • From subscription to reader
  • From reader to area
Situation Mapping Java code
There is not a single function that uses the direction 'to-one'.

Example: Going from Reader you'll never have to access the related area.

No mapping. public class Reader { }
All functions have the 'one' available. This often occurs with an identifying 'to-one' relationship from the SNF.

Example: A subscription is only accessed through reader. Every reader knows its subscriptions. The backward implementation, from subscription to reader, is redundant.

No mapping either public class Subscription { }
The 'to-one' relationship is used.

Example: from Subscription to Newspaper.

Create a variable that points to the 'one'. public class Subscription { private Newspaper paperObj; }

to-many

Situation Mapping Java code
There is no function that uses the 'to-many' direction.

Example: Coming from Newspaper you'll never have to access all readers.

No mapping. public class Newspaper { }
There is an access path using the 'to-many' relationship. Sometimes that relationship is used to find one specific instance.

Examples:

  • Retrieve all subscriptions for a given reader.
  • Retrieve the subscription for 'The New York Times' for a given reader.
Implement the to- many relationship as a Hashmap. public class Reader { Hashmap subscriptionsObj; // key: newspaper name // data: instance of Subscription }
There is an access path using the 'to-many' relationship. The sequence is important.

Example: For a delivery boy the sequence of readers in his route is important.

Implement the to-many relationship as a Vector. public class Area { Vector readersObj; }

Till next week,
Nut

IM Astrid Lindgren, 1907 - 2002