Database Reference
In-Depth Information
In Listing 6-7, we use the
Load()
method (see the recipes in Chapter 5) to ensure that the collections of related
products are loaded. Unfortunately, this means that we will end up with many additional round trips to the database.
We might be tempted to load all of the rows from the Product table up front and hope that relationship span would fix
up the associations. However, relationship span will not fix up entity collections, only entity references. Because our
associations are many-to-many (entity collections), we cannot rely on relationship span to help out and we have to
resort to using the
Load()
method.
Following is the output of the code in Listing 6-7. From the first block of code that inserts the relationships, we
can see that a Pole is related to a Tent, and a Tent is related to Ground Cover. The transitive closure for the products
related to a Pole includes a Tent, Ground Cover, and Pole. Pole is included because it is on the other side of the
relationship with Tent, which is a related product.
Products related to Pole
Tent
Ground Cover
Pole
6-4. Modeling a Self-Referencing Relationship Using Table per
Hierarchy Inheritance
Problem
You have a table that references itself. The table represents several different but related kinds of objects in your
database. You want to model this table using Table per Hierarchy inheritance.
Solution
Suppose that you have a table like the one shown in Figure
6-5
, which describes some things about people. People
often have a hero, perhaps the individual who inspired them the most. We can represent a person's hero with a
reference to another row in the Person table.
Figure 6-5.
Person table containing people with different roles
Each person has some role in life. Some people are firefighters. Some people are teachers. Some people
are retired. Of course, there could be many other roles. Information about people can be specific to their roles.
A firefighter is stationed at a firehouse. A teacher teaches at a school. A retired person often has a hobby.