This example shows a join between three tables. A location belongs to a region which belongs to a division. Many locations can belong to a region, and many regions can belong to a division. For example; the "Boston" location belongs to the "New England" region, which in turn is under the "North America" division.
The tables are LOCATION, REGION, and DIVISION. The ddl for creating these tables is in table-definitions.sql The LOCATION table has a foreign key column, REGION_ID, that points to the REGION table. Simularily the REGION table has a foreign key column, DIVISION_ID, that points to the DIVISION table. The region and division are marked as
Division division = new Division(); // Create a new division
division.setDivisionName("North America"); // set the divisions name
division.store(); // Insert the division to the database
Assume that the new division was created with a primary key of "10".
Region region = new Region(); // Create a new Region
region.setDivisionId(10); // set the foreign key to the DIVISION table
region.setRegionName("New England"); //
region.store(); // Insert to the region table
Assume that the new region was created with a primary key of "20".
Location location = new Location(); // Create a new location
location.setRegionId(20); // set the foreign key to the REGION table
location.setName("Boston"); //
location.store(); // Insert to the location table
Assume that the new location was created with a primary key of "30".
Location location = new Location(30); // Get the existing location
System.out.println("Location name = " + location.getName() );
System.out.println("Region name = " + location.getRegionName() );
System.out.println("Division name = " + location.getDivisionName() );
Location location = new Location(30); // Get the existing location
location.setName("Boston downtown");
location.store(); // Updates the location table
Note: You cant modify a region's, or division's properties through the location
class. The region and divisions are marked "readOnly" since they may
have many locations belonging to them.