Re: OOP - a question about database access

From: AndyW (awatson__at_attglobal.net)
Date: 11/01/03


Date: Sun, 02 Nov 2003 11:40:44 +1300

Welcome to the forum :)

Matching an OO application to a relational database is a pretty common
scenario. There are very few OO database developers around these
days.

As an aside, there is a small OO database program called PSE Pro that
I think is available in C++ and Java. Last I saw it had a 30 day
license. Not sure if its still around, you may have to google search
for it.

Using a relational Db with an OO application is kind of like mixing
metaphors. In general a normalised RDB will need a translational
layer to manipulate the data into something usefull, where an OO
database does not.

>From the OO point of view, each RDB table is normally treated as a
sequence of data containing zero (empty) or more elements.

Typically, one would use a bunch non-OO code sitting in a framework
that handles the CRUD side of the database as well as access to each
data sequence from the application.

You dont actually have any physical entity data in your database, so
its unlikely that you will be able to define an object. The closest
you might come would be to have an object called 'Match' containing
the attributes date, home team, visitor team, and start time.
However, I suspect that you may have a bit of a problem when it comes
to defining some operations other than getters/setters. The reason
being is that a match is an abstract human concept defining an event
that is occuring rather than a tangible object defining a concrete
item. Abstractions tend to have no operations.

So its likely with this data set that you will end up with some
classes that just contain some operations which will be used to
manipulate the sequence (data from the table).

If you would like to change your data in to an OO form, it is actually
possible, although you will have a few more attributes in your
database.

There are two candidates for being objects. These are home team and
visitor team. Both are children of the abstraction team. You would
need attributes defining some details about the team, and some
operations. For your RDB the abstraction is likely to be the
database table, so you may have a table called Team, with attributes,
Name, location etc. Note that the attribute Home/Away does not
belong in the Team object. I'll explain why below.

The original db table would be renamed match or sporting event. This
is an association table containing the attributes data, home team
foreign key, visitor team foreign key, start time. You also place the
attribute Home/Away here. I would make it a boolean flag called
AtHome and assign it to refer to only one of the teams. If the flag
is set, then that team will be at home, and by definition the other
will be away.

This gives an object model as follows:

   Abstract Object: Team
   Concrete Object: Home Team inherits from team
   Concrete Object: Away Team inherits from team
  
   Link Association: Match (links the home team to the away team).

    Home Team ------------------------------------ Away Team
                                  - -
                                    - -
                                       -
                                  Match

   Now that we have modeled it like this, the next step would be to
 remove the inheritence as the abstraction in ambiguous.

    Team ------------------------------------ Team
                              - -
                                - -
                                   -
                               Match
   
    (here I have put two instances of team because its hard
        to draw it correctly with ascii. The line should come out
        of the left hand team and go back into it).
  

   The implementation would result in your team table being loaded
   into a sequence of teams, and your match table being loaded into
   a sequence of matches.

   If you have used primary keys in your database tables eg. match Id
and Team Id. And the match table has the two foreign keys Home Team
Id, Away team Id. Then when you load this data into the sequence
the relationship will be created for you. That is the primary key
becomes the object ID (OID). Since the OID is always unique for an
object (just like a primary key), You have effectively
automatically created an index on each of your sequences.

Thus the sequences can be loaded from the DB using the appropriate
SELECT...FROM....ORDER BY primary key asc (or whatever) and the
appropriate STL type code (ordered set) can be used for access.

These are just my thoughts.. Hope they help.

On Fri, 31 Oct 2003 18:49:07 GMT, Tim <nocomment@rogers.com> wrote:

>If this is the wrong newsgroup, can someone please point me to the
>correct one and disregard this.
>
>I've been programming for 20 years but I'm trying to understand OOA and
>OOD now. What I've decided to do is try to take a little database I have
>that includes a table with a sports league's schedule and try to use
>this to develop a little application to help me understand the concepts
>and proper techniques of OOA, OOD and OOP.
>The columns in the table are date, home team, visitor team and starting
>time.
>(I'm going to use Java in a JDeveloper environment and the database is
>an MSAccess using a JDBC/ODBC bridge)
>
>So, how would you divide the various functions regarding interfaces,
>inheritance, etc.
>
>Does the object that represents a game's information have the methods to
>connect to the database, execute the query and retrieve the rows from
>the resultset or are they done in the calling program or elsewhere? Does
>each column have it's own get method or do you pass all of the game's
>info to the calling class by a container once retrieved? Do you get all
>rows of the result set from one call to the method of the object or do
>you use a GetNextRow method and check a return code?
>Are the rules for this hard and fast because I've looked around and
>there doesn't seem to be anything definite considering this type of
>thing must be fairly common.



Relevant Pages

  • Re: How should I generate a primary key?
    ... wrong with using IDENTITY or SEQUENCE? ... I asked in a separate thread "Is there a database independent way of ... value and primary key values should not be changeable for any reason. ... I have normally called this simply reference data. ...
    (comp.databases)
  • Re: problem using identity column as primary key
    ... >> I am thinking of creating an identity column to use it as primary key ... More and more programmers who have absolutely no database training are ... the gap in the sequence is not filled in and the sequence ... vin CHARNOT NULL REFERENCES Motorpool); ...
    (microsoft.public.sqlserver.programming)
  • Re: How should I generate a primary key?
    ... database engines and their extensions over time. ... Please go back and read Codd. ... The primary key cannot have any meaning to it unless its creation is always ... be committed before the next person could get the next sequence. ...
    (comp.databases)
  • Re: do all primary keys use autonumber
    ... Here's a quick demo of why for Jet you must choose a PRIMARY KEY ... INSERT INTO [Sequence] VALUES ... INSERT INTO Test1 ... In my test timings, after compacting the database, both versions ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Concatenate and Null Values -- Features
    ... I have to say it's hard for me to see how a sequence number such as I have ... I should probably mention that this database is unlikely to generate more ... I wonder if storing 07-01 (probably without the dash, ... I think I know what you're saying about learning by teaching. ...
    (microsoft.public.access.reports)