Object-Oriented Apps in a Relational Database
By Brian Jepson
Bridging the world of object-oriented development with relational databases can be difficult. Suppose you're building large, complex applications, with many domain objects. One way to hook up your objects to the database is to put SQL code in various parts of your objects. Unfortunately, this makes a mess of things in a number of ways. For one, your classes become closely coupled to the database (which is undesirable, because most object-oriented design patterns seek to reduce coupling). Also, you may need to compromise your database design, since you may end up modeling your tables on your class design rather than going through a well-normalized database design.
The biggest complications are due to the fact that objects aren't usually structured in the same way as database tables. In a database, a one-to-many relationship between tables is modeled by defining the child table with a reference to the parent table's primary key, as shown in
Figure 1. In an object model, the same relationship can be modeled by defining a collection in the parent object, as shown in
Figure 2. This is just one example of what's called the impedance mismatch between objects and relational databases. This problem can be avoided by using an object-oriented database management system (OODBMS), but it's a fact of life that many of us use relational databases.