tags:

views:

188

answers:

5

When programming an OO program using a SQL database back-end, do objects' attributes correspond with the rows in the tables? Or more than that? I don't quite understand how objects' attributes relate to data in tables. Thanks for your help.

+7  A: 

It really depends which orm you are using, however, in general the idea is:

class = > table
instance => row
attribute => column
ennuikiller
A: 

You need to be more specific, and possibly do a little bit of research on what is available on your selected platform. While there are ways of mapping various Object Oriented languages onto databases, there is no "one-size fits all" approach.

Adam Batkin
Could you provide a "positive" ("do this") kind of example? Your answer is kind of "negative".
S.Lott
*Nobody* has figured it out, yet. Some of the smartest people in the industry have tried, and failed. A negative answer is the only one that's sincere. You could say "just use Hibernate and everything will be fine", except it won't and you would be lying.
Jörg W Mittag
@Jörg W Mittag: Some people are very successful, what do they do that works? The "no one size that fits all" is always true for all software architecture problems. However, some hint as to what is better or less risky or more helpful could be more useful than repeating the obvious "no one size fits all."
S.Lott
@S.Lott that's not really fair, I did provide a "do this" example: fix the question! The question is very specific, it ask for an answer that is very specific for each possible technology (of which there are many) and yet the question is waaaay overly broad. The only possible answer is "go learn about OOP and databases, maybe read about a few ORMs" but that's the answer to a very different question, that wasn't asked.
Adam Batkin
+6  A: 

ennuikiller mentioned some things, but an object may also be store over multiple rows and tables because there may be no correlation because of how data is modelled and normalised.

It's not straightforward... some reading Object-relational impedance mismatch (Wikipedia)

gbn
thanks gbn, I forgot to mention the complexity of the data-model.
ennuikiller
I tried to read that article (actually before I posted this question) but I couldn't grasp it. Can someone summarize it or is there a simple example that illustrates it?
ChrisC
+2  A: 

Read a little about Object-relational_mapping

a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

For Java or .NET, the hibernate project provides relational persistence.

Hibernate is a powerful, high performance object/relational persistence and query service. Hibernate lets you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.

Unlike many other persistence solutions, Hibernate does not hide the power of SQL from you and guarantees that your investment in relational technology and knowledge is as valid as always.

gimel
+2  A: 

My answer is no.

The goal of OO design is to optimize for the ease of specifying program behavior. The goal of database design is to optimise for ease of data storage/retrieval. These goals are very different and can and will lead to very different decompositions of the problem domain.

It's possible to map one to the other, but then you will encounter the impedence mismatch, as others have mentioned, which is basically the technical consequence of the different goals of the two models.

Jorg mentioned the "Vietnam of computer science" article in the comments, which is worth a read. You can skip the historical stuff on Vietnam, if you're short on time.

Rik
Isn't it somewhat common to have an OO program with a SQL back-end?
ChrisC
Yes, it is. It is also common, to have massive problems. In fact, Object-Relational Mapping has been widely dubbed as the "Vietnam of Computer Science", because we have been at this war for decades, with no end in sight.
Jörg W Mittag