Question

I have been puzzled by this for a while now. My question is, how does object-oriented programming and database management work together? I can't seem to find much on how the two work together. I wanted to make a database driven application and I currently have a good book on object oriented programming that has a process for creating applications using object-oriented methodologies but I just get confused when we start involving databases.

Is a class diagram essentially a database design? Can we still query the database with SQL just like we do with relational databases? How is the data file stored and in what format? Are classes the equivalent to tables and their attributes equivalent to columns?

Thanks in advance I appreciate any responses and I did try google and the similar questions offered.

Was it helpful?

Solution

It seems like you're coming up against the Object Relational Impedence Mismatch.

Is a class diagram essentially a database design?

No it isn't, the database model and your OO model may well be very different.

Can we still query the database with SQL just like we do with relational databases?

Yes you can, but you'll change the data into objects, see the Repository Pattern on how to do that.

How is the data file stored and in what format?

The same way and format RDBSs have been storing data for decades, I won't muddy the water by suggesting you look at the growing popularity of NoSQL systems.

Are classes the equivalent to tables and their attributes equivalent to columns?

Not necessarily, you'll be mapping the data in tables to objects in an OO design. Sometimes they might map almost directly. You might also, for example, have the data in 3 tables mapped to one object.

OTHER TIPS

ORM(Object Relational Mapping) in Hibernate is a good example for your clarification!

For the questions like : Is a class diagram essentially a database design? and Are classes the equivalent to tables and their attributes equivalent to columns?.. Yes Let me explain with you an example:

class Department
{
     private int departmentNo;
     private int departmentName;
     private Set<Student> students;
}

class Student
{
    private int studentId;
    private int studentName;
    private Department department;
}

using these entities, Two database tables will be generated with hibernate. by default,

Department: departmentno(int),departmentname(varchar(255))
Student: studentid(int), studentname(varchar(255)),departmentno(int) [foreign key]

are generated.

List of Students of a particular department can be retrived by,

Department department = ......; // hibernate related thing which hit the database
Set<Student> = department.getStudents();

I have been reading on this for some years now. I love the OOP approach and I also develop database-driven applications. Here are my findings:

  1. OOP and Data Model in a sense are two separate worlds. This is because RDBMS have existed for years before OOP was adopted.

  2. OOP focuses on classes and objects; RDBMS focuses on data storage and retrieval.

  3. If you spend time building classes that model your tables, its fine,but that would add a performance overhead to your application and increase the development time.

My Advice for .Net Developer: Use that classes that have already been provided by the .Net framework under the System.Data namespace. A good knowledge of these classes would reduce the size of your code to the minimum. Only create a few custom classes when strictly necessary

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top