Foreign Key Mapping Pattern

This pattern belongs to Object-Relational Structural Patterns Catalog and this Catalog belongs to Patterns of Enterprise Application Architecture.

Intent

Maps an association between objects to a foreign key reference between tables.

Key points

  • A Foreign Key Mapping maps an object reference to a foreign key in the database.
  • In java programming, objects can refer to each other directly by object references.
  • In a relational database, a FOREIGN KEY is a key used to link two tables together.
  • In a relational database, a FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
  • The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

Explanation

Let's understand Foreign Key Mapping pattern with an example.
Look at the following two tables:
"Persons" table:
PersonID LastName  FirstName  Age
  Hansen  Ola  30
2  Svendson  Tove  23
3  Pettersen  Kari    20

"Orders" table:
OrderID OrderNumber   PersonID
177895    3
244678      3
322456    2
424562    1

Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.

How It Works

The obvious key to this problem is Identity Field. Each object contains the database key from the appropriate database table. If two objects are linked together with an association, this association can be replaced by a foreign key in the database. Put simply, when you save an album to the database, you save the ID of the artist that the album is linked to in the album record.

Implementation

Foreign Key Mapping mainly used in one-one, one-to-many and many-to-one associations.
Let's write code sample Foreign Key Mapping :
Artist has multiple albums so create artist reference variable in Album class indicating that Artist is the Foreign Key Mapping.
class Artist {
private long id; private String name; public Artist(Long ID, String name) { super(ID); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Album {    private String title;    private Artist artist;    public Album(Long ID, String title, Artist artist) {       super(ID);       this.title = title;       this.artist = artist;    }    public String getTitle() {       return title;    }    public void setTitle(String title) {       this.title = title;    }    public Artist getArtist() {       return artist;    }    public void setArtist(Artist artist) {       this.artist = artist;    } }

Comments

Post a comment