Why Object-Oriented Databases

Applications such as CAD, multimedia, engineering design and GIS need complex data and operations on it, and the relational model with its flat tables handles it badly. An object-oriented database stores objects directly, so the database and the programming language share one model (no impedance mismatch).

Object Identity

Every object has a unique object identifier (OID) that is system-generated, immutable and independent of the object's values. Two objects may hold equal values and still be different objects. Objects refer to each other by OID, which makes sharing easy.

Object Structure and Type Constructors

An object has a state (its attribute values) and a behaviour (its methods). The state is built with type constructors from atoms (integer, string, Boolean):

  • Tuple (struct): a record of named fields, for example address(street, city, pin).
  • Set, bag, list and array: collections; a set has no duplicates, a bag allows them and a list keeps order.
  • Reference: a field holding the OID of another object.

These can be nested to any depth, which gives complex objects.

Encapsulation, Methods and Persistence

Encapsulation hides the structure of an object and exposes only its operations (methods), so data is changed only in controlled ways. A class has an interface (the visible operations) and an implementation. Persistence means that objects survive after the program ends. In an ODBMS objects can be transient or persistent, made persistent by being named (a root object) or by being reachable from a persistent object.

Type Hierarchies and Inheritance

A subtype inherits the structure and methods of its supertype and may override a method (polymorphism). Multiple inheritance lets a type have several supertypes, which can cause naming conflicts that the language must resolve.

Definition: Object identifier (OID). A unique, system-generated and unchangeable identity given to an object when it is created, independent of the object's attribute values.

Table 1.3: Relational and object-oriented models

Point Relational Object-oriented
Data unit Row in a table Object with identity
Identity Primary key value OID, independent of values
Structure Flat, atomic values Complex, nested
Behaviour Outside the database Methods stored with data
Relationships Foreign keys References and OIDs

Example 1.3: Identity and sharing of objects

Problem: A student object has the fields name and an address object (city). A copy of the student is made so that both share the same address object. The city of the copy is changed to "Pune". What does the original show, and how does it differ from a deep copy?

Solution:

  1. A shallow copy copies the OID of the address, so both students refer to one address object.
  2. Changing the city through the copy changes the shared address, so the original also shows "Pune".
  3. A deep copy creates a new address object, so changing it leaves the original as "Delhi".

Result: With sharing the original shows Pune; with a deep copy it keeps Delhi. Equal values (==) do not mean the same identity (is).

import copy
class Address:
    def __init__(self, city):
        self.city = city
class Student:
    def __init__(self, name, addr):
        self.name, self.addr = name, addr
a = Student("Asha", Address("Delhi"))
b = copy.copy(a)            # shallow: shares the address object
c = copy.deepcopy(a)        # deep: new address object
print("Same address object (b):", b.addr is a.addr)
print("Same address object (c):", c.addr is a.addr)
b.addr.city = "Pune"
print("Original city after change via b:", a.addr.city)
print("Deep copy city:", c.addr.city)
x, y = Address("Goa"), Address("Goa")
print("Equal values but same object?", x.city == y.city, x is y)

Output

Same address object (b): True
Same address object (c): False
Original city after change via b: Pune
Deep copy city: Delhi
Equal values but same object? True False

Common mistakes

  • Using a changeable key value as identity; an OID never changes.
  • Confusing equal values with the same object.
  • Exposing attributes directly and breaking encapsulation.
  • Ignoring that a shallow copy shares sub-objects.

Key takeaways

  • OID is unique, system-generated and immutable.
  • Type constructors: tuple, set, bag, list, array and reference.
  • Encapsulation hides structure and exposes methods.
  • Persistence is by naming or by reachability.

Practice questions

  1. What is an OID?
  2. What is encapsulation?
  3. Name three type constructors.

Answers to the practice questions

  1. A unique, immutable, system-generated identifier of an object.
  2. Hiding the internal structure of an object and exposing only its operations.
  3. Tuple, set and list (also bag, array and reference).

Sources

  • Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit I