Platinum Solutions Corporate Website


A Good Object Model is not necessarily a Good Relational Model

The answer you entered to the math problem is incorrect.

I was recently involved in a discussion regarding the relationships between database tables and how they differ from the relationships between objects.

I should first say that I come to Platinum Solutions and this blog space not as a ‘java programmer’, but as an applications developer with a rich history (~25 years) of data-driven development. My only experience with Java until now has been with the late unlamented SilverStream, which lived prior to J2EE. More on this in a later entry…

The discussion centered around certain key pieces of data and the distribution of same within the database. Imagine data in a grandparent (GP) –parent (P) –child (C) relationship. The object model had relationships described between GP and P and between P and C, but no direct way for C to reach GP except through P. While this may be perfectly acceptable in an object model, this will be a problem in a relational database when you try to create a query that relates child to grandparent, and a real mess when you try to pull data from two children with the same grandparent but different parents.

For example:
The grandparent record is a list of crimes committed
The first parent record is victims of the crime
The second parent record is suspects of the crime

Let’s say the key in the ‘crime’ table is the state abbreviation and a sequential number; this is the ‘glue’ that holds the case together.

The key for the first parent (victim) is a sequential number. It has a foreign key back to the crime table. Ditto for the second (suspect) table. Each of these tables has a child table of addresses, as they may both be known criminals and give false addresses, or they may have a date element to reflect movement over time; whatever. The address tables have foreign keys to their respective parent tables.

Let’s say someone wants a list of cases and apartment numbers where the victim lived in a particular apartment building. If you have the case number in the address table, it’s relatively
simple:

SELECT CASE.CASE_NUMBER, VICTIM_ADDRESS.APARTMENT_NUMBER
FROM CASE, VICTIM_ADDRESS
WHERE VICTIM_ ADDRESS.STREET_ADDRESS = ‘1257 TENEMENT’
AND CASE.CASE_NUMBER = VICTIM_ADDRESS.CASE_NUMBER

without case number in the grandchild record, it gets more complicated:

SELECT CASE.CASE_NUMBER, VICTIM_ADDRESS.APARTMENT_NUMBER
FROM CASE, VICTIM, VICTIM_ADDRESS
WHERE VICTIM.ADDRESS = ‘1257 TENEMENT’
AND CASE.CASE_NUMBER = VICTIM.CASE_NUMBER
AND VICTIM.VICTIM_ID = VICTIM_ADDRESS.VICTIM_ID

Now let’s say someone sees the report and asks to include suspects living in the same building. The revised second query gets unwieldy rather quickly and I’m not going to try to reconstruct it. If you want cases where either (not just both) the suspect OR the victim lives in the building you have to modify the query to use outer joins. Good luck with the second query. If you can even get it to work, you can take a long lunch while it does the query on a database of any real size.

This is just the tip of the iceberg. The result set may need to add elements from other tables related to the case but unrelated to the tables discussed so far.

The moral of the story? Don’t let a pure object-related design go into development without the reality check of comparing the object model to the relational model to the use case matrix.

Comments

Patrick S. Dunn Wed, 1969-12-31 20:00

In general I would say there is no such thing.  What you usually find, in a mismatch scenario, is a data model (logical model to be more precise) that is incorrect. 

If your logical model correctly structures functional dependencies the mapping is trivial.  You simply work with creating objects that act as specific views to your data (then insert ORM framework of choice to simplify SQL calls).  

This really isnt the same as direct table mapping, but its close.  If DBMS vendors had better support for updateable views, you'd really map your objects to the views you intended your application to use. 

The hard part is noticing when there is something wrong in your logical model and fixing those table declarations, prior to writing 1000's of lines of code trying to simulate a correct model.

Matt Harrah Wed, 1969-12-31 20:00

The flip-side of your claim is equally true: a good relational model is not necessarily a good object model.  Too often have I seen inexperienced programmers take their legacy database tables and declare JavaBeans that mimic their table structures in the false hope that this will make things easy, because they will reduce the impedence mismatch between their tables and their objects.  The usually find that they still have to do much work to map data from objects to tables anyway, and they make the rest of their system harder to work with than they needed to because they chose a design for their objects that modeled their tables rather than the objects they really needed.

In an ideal world, analysts would construct a conceptual data model first, then build a relational model and an object model that both optimally implement the conceptual model.  Ah, if only we all lived in that ideal world!

Patrick S. Dunn Wed, 1969-12-31 20:00

I could fill a book with my gripes about this.  The big issue with relating data in network models (i.e. object models) seems to be pointers and projection.

Post new comment

Please solve the math problem above and type in the result. e.g. for 1+1, type 2.
The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options