oopdb4oobject-oriented-database

How to design many-to-many relationships in an object database?


I thought it was about time to have a look at OO databases and decided to use db4o for my next little project - a small library.

Consider the following objects: Book, Category.

A Book can be in 0-n categories and a Category can be applied to 0-m Books.

My first thought is to have a joining object such as BookCatecory but after a bit of Googling I see that this is not appropriate for 'Real OO'.

So another approach (recommended by many) is to have a list in both objects: Book.categories and Category.books. One side handles the relationship: Book.addCategory adds Category to Book.categories and Book to Category.books. How to handle commits and rollbacks when 2 objects are been altered within one method call?

What are your thoughts? The second approach has obvious advantages but, for me at least, the first 'feels' right (better normed).


Solution

  • If you use object database you don't need to care how relations are stored in database. You define classes and relationships between them. Please read the reference guided to your database. Examples of relationships:

    n:n attribute, referencing from the parent
    ------------------------------------------------------------------
    class Person {
        List addresses;
    }
    
    class Address {
    }
    
    
    n:n attribute, referencing from the child
    ------------------------------------------------------------------
    class Person {
    }
    
    class Address {
        List persons
    }
    
    n:n attribute, bidirectional references
    ------------------------------------------------------------------
    class Person {
        List addresses;
    }
    
    class Address {
        List persons
    }