Computer Science › Section J · Database systems › Topic J.4

Object-oriented databases

What if a database stored objects, complete with their links and behaviour, instead of rows in tables? Object-oriented databases do exactly that, and their ideas now live on inside many mainstream systems.

From tables to objects

"Object-oriented programming" showed how a program can be built from objects, each an instance of a class that bundles data with methods. An object-oriented database applies the same idea to stored data. A film archive might define three classes: Film, with a title, a year and a running time; Person, with a name and a birth year; and Credit, recording that one person did one job, such as director or editor, on one film. Instead of rows in tables, the database holds objects of these classes, linked to one another: each person object is linked to its credits, and each credit is linked to exactly one film.

Links instead of matching values

In a relational database, a credit row refers to a film by repeating the film's key value, and a join finds the matching rows. In an object-oriented database, a credit holds a direct reference to the film object itself. Every object has an object identifier, a unique internal identity assigned by the DBMS that never changes, even if every attribute of the object does. Applications do not manage these links by hand: when a new credit is stored, the program says which person and film it belongs to, and the DBMS records the associations, perhaps as a linked list of credits for each person. Finding related data becomes a matter of following references.

Worked example: one person's films, two ways

Task: list the films on which Rosa Ilic worked. Relational steps: (1) Select the Person row where Name = 'Rosa Ilic' and note her key, say 17. (2) Join Credit with Film on the film key. (3) Select the credits whose person key is 17. (4) Project the film titles. Object-oriented steps: (1) Look up the Rosa Ilic object. (2) Follow its links to her credit objects. (3) From each credit, follow the link to its film and read the title. Both give the same titles. The relational version matches values across tables; the object version walks from object to object, which suits data that is naturally explored by navigation, such as parts inside assemblies inside machines.

Transient and persistent objects

Normally, objects live only as long as the program that creates them. When the program ends, its memory is released and the objects are gone: they are transient. Objects placed in a database must outlive the program, so that the next program, or the same one run tomorrow, finds them intact. Such objects are persistent. Providing persistence is a central job of an object-oriented DBMS. It must turn objects, including their references to other objects, into a form that can be written to mass storage, and rebuild them faithfully when they are read back. A relational DBMS has an easier task here, because its rows are already plain values.

Worked example: persistence in Python

Python's standard shelve module stores objects in a file under string keys, a small taste of persistence. Program A: 1. import shelve 2. define a class Person whose __init__ sets self.name = name and self.films = [] 3. p = Person('Rosa Ilic') 4. p.films.append('Harbour Lights') 5. with shelve.open('archive') as db: db['p1'] = p. Program B, run later as a separate process: 1. import shelve 2. define the same Person class 3. with shelve.open('archive') as db: print(db['p1'].name, db['p1'].films). Output: Rosa Ilic ['Harbour Lights']. The object outlived Program A. Leave the class out of Program B and loading fails with AttributeError: Can't get attribute 'Person', because the object's data was saved but its class was not.

One paradigm from end to end

Supporters of object-oriented databases point to consistency of design. With an object-oriented application and an object-oriented database, the whole system, from application to DBMS to stored data, is built in one paradigm: objects sending messages to objects. Pairing an object-oriented or imperative program with a relational database instead means constant translation, turning rows into objects on the way in and objects into rows on the way out. The awkward fit between the two is often called the object-relational impedance mismatch, and hand-written translation code has long been a source of bugs.

Hiding complex data inside objects

Relational designs fix each attribute's type across the whole database, which makes unusual data awkward. Postal addresses are a good example: their parts and their order differ from country to country, so any fixed set of columns fits some addresses badly. An Address object can store whatever parts a particular address has and offer methods such as label() for an envelope or city() for a report, hiding the details inside. The same encapsulation helps with multimedia. Retrieving an audio clip, a video or a 3D scan can use the same general operations as retrieving an address, because each object knows how to handle its own contents.

Objects that answer questions

Because objects carry methods as well as data, an object-oriented database can store intelligent objects rather than passive records. A Person object might offer filmography(), returning the person's films in date order, and a Film object might offer crew(), listing everyone credited on it. An application that needs a person's career simply asks the person object, instead of assembling a query across several tables. The rule for producing that answer lives in one place, the class, so every application using the database gets the same behaviour, and a change to the rule is made once.

Object databases today

Commercial object database systems appeared from about the late 1980s and found uses in specialised areas such as engineering design, where data is deeply nested and explored by following links. They did not displace relational systems, which kept a mature standard language and a large body of tools and skills. Instead, the ideas spread. Object-relational DBMSs such as PostgreSQL let designers define their own data types. Object-relational mappers, such as SQLAlchemy for Python and Hibernate for Java, generate the translation between classes and tables automatically. Document databases, met in "Data mining, big data, and NoSQL", store nested, object-like records directly.

Common mistakes to avoid

Thinking an object identifier is just a primary key: a key is a value that could be edited, while an identifier is internal identity that never changes. Assuming objects persist on their own; ordinary program objects are transient. Forgetting that persistence must save the links between objects, not only their attributes. Believing object databases replaced relational ones; relational systems remain dominant, with object ideas added on top. Treating shelve as a full database: it has no query language, no transactions and no safe multi-user access, and, like Python's pickle, it must never open files from untrusted sources.

Key takeaways

An object-oriented database stores objects of classes, linked by references rather than matching key values. Each object has a permanent object identifier, and the DBMS maintains the links. Its central extra task is persistence: saving objects, links included, beyond the life of the program. Supporters cite one paradigm from end to end, encapsulation of complex and multimedia data, and stored methods that answer questions. Relational systems stayed dominant, but object ideas live on in object-relational DBMSs, object-relational mappers and document databases.

Flashcards

Flashcard

Object-oriented database

A database that stores linked objects of classes

Data and methods together.

Test your understanding

Question 1 of 5

What is a persistent object?

Question 2 of 5

How does an object-oriented database typically find the films a person worked on?

Question 3 of 5

Program B opens a shelf holding a Person object but never defines the Person class. What happens?

Question 4 of 5

Which tool automatically translates between Python classes and relational tables?

Question 5 of 5

Why do supporters say object databases suit multimedia collections?

← Previous topicNext topic →