Object-Relational Mapping Using Hibernate (MySQL+Java+Netbeans IDE): Part I

What world are you living in? Relational world or object-oriented world? Maybe you do not know what your answer is. Let ask more specific questions.

Are you modeling your software application using object-oriented approach?, are you using UML (Unified Modeling Language)?, and are you implementing your application using Java, C++ or C#? If you say “yes” to one of these three questions, you are living in an object-oriented world.

Hold on. Are you sure if OO is the only world where you are in? Have you ever used a database for storing data? If you say “yes”, I bet that more than 80% of you guys have used relational database management systems (RDBMS) such as MySQL, MS SQL Server, and Oracle. RDBMSs are systems that provide functionalities and tools (with SQL capabilities) for creating a database, and controling and managing data in the database. The database in this type of systems is a relational database which is developed via the use of Entity-Relationship (ER) or Enhanced-Entity-Relationship (EER) diagram.

So, what do you usually do if you need to store data for your software application? You can still write your application using Java or Object-Oriented approach and use MySQL which is a relational database for storing data. How? Well, you could embed SQL queries within your Java application by using JDBC or Java Database Connector or J/Connector for MySQL. This is also what I usually do.

(Storing data from software applications to be used between sessions is known as providing persistence to data.)

So, you may ask what my point is. Well, the thing is that if you have to embed SQL queries into Java applications, you have to specify SQL queries in advance statically and possibly dynamically or on the fly). To be able to write SQL queries, you certainly must understand the underlying model of the relational database. What if you are not the one who designs the database or you do not know much about the relational model? In addition, an inclusion of SQL queries within your application unavoidably add more complexity to your programs.

Isn’t better if you can just focus on writing your Java programs and the data that you want to store or retrieve can be inserted and retrieved from the relational database automatically without your notice? The answer is “yes”. What you need is a middle man which maps your OO application to relational database (RDB), for example, Java Persistence API, Oracle’s Toplink, Java Data Objects and Hibernate (NHibernate for .NET). These middle men are called Object-Relational Mapping frameworks.

An object-relational mapping framework provides developers transparent persistence to data in their software application development using OO and relational database. In other words, the developers can concentrate on their application development without concerning the underlying relational database (separation of concerns).

OK OK. you may want to ask this question. Why do we need this framework to map OO to relational? Well, the answer is easy. Although OO model looks really similar to relational models or ER/EER models. They are quite different.

In the next part, I will write about their differences, how we can map them, and how to use Hibernate.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s