Share |
(Cached)
Refresh Print

Introduction to ORM

ORM stands for Object relational Mapping. ORM is an attempt to map the notion of object and relational world so that they can talk to each other in a easy way. Any non trivial application has a database behind it and Java applications are no exception. In fact if we look closely into any application, one will realize that the application gets more or less modeled around the data model. In database technology, relational database are the clear winners. Other database technologies has come and gone. Relational concept of data management was first introduced by E.F. Codd in 1970. An analogy for relational model can be drawn with spreadsheets. Each sheet represents a table and the columns in the sheet represent the table attributes. Each instance of data is represented by the rows. The data in different sheets are connected with each other by referring to the data point using the sheet number, column number and row number. This is what is called as foreign key relationship in database technology. In fact most of the GUI interfaces to database show the data in a spreadsheet format.
To interact with the database, Standard Query Language(SQL) has emerged as the standard way. The SQL standards are controlled by ANSI. However there are still proprietary variations to it. SQL provides two types of mechanism:
  • Data Definition Language (DDL) : Provides ways to create and alter tables.
  • Data Manipulation Language (DML) : Provides ways to manipulate and retrieve data. It includes inserting, updating and deleting data.

To interact with the database, the applications has to issue SQL to the database. How to issue SQL is proprietary to each database. They have their own API's exposed for this and the API's might be written in different languages. For example a database written in C language. might expose C based API's. Considering that the data independence is considered a virtue for any application, it would be a lot of work for an application developer to understand the interfaces for each of the database and implement it. To solve this kind of porblem, Java has come up with JDBC API's.

JDBC is the most popular way of connecting to databases in Java. It's an interface based API where the implementation for each database is provided by the drivers for particular database. Though JDBC is very popular, it is inherently relational in nature. The basic problem is the mismatch in conceptual level between relational technology and Object Oriented Technology. Java being a pure Object Oriented Language, this mismatch is important to deal with. This mismatch is also known as Object relational mismatch. ORM tries to solve this mismatch.

Let's see the kind of mismatch that are there:

Inheritance

Java supports inheritance. For example we might have User class from which Student and Teacher class is derived.
User
public class User{ private String Name; //Setters and getters }

Student
public class Student extends User{ private double percentage; //Setter and Getter }

Teacher
public class Teacher extends User{ private int exprienceYears; //Setters and Getters }

Now think for a moment how you are going to map these classes to the table structure. ORM frameworks adopt different strategies to solve this, which can be seen at Hibernate section.

Also with this comes the mismatch in terms of polymorphism. A reference of User type can refer to an object of Student or Teacher. Also a list might contain a mix of Teacher, Student and User objects. How you build those list by querying database. The ORM frameworks has to somehow differentiate that the data is belonging to User or Student or Teacher.

Granularity

The granularity problem comes when the number of classes mapping to number of tables in the database do not match. For example let's say we have the User class which has an Address object
public class User{ private String Name; private Address address; //Setters and getters }

Address
public class Address{ private String city; private String country; //Setters and getters

Also the table structure for User is
Table USER:
NAME CITY COUNTRY

There is one table but the data is sitting in two objects. The same problem can come the other way round also where you have two tables and one class containing all the data points. ORM frameworks has to care of this mismatch in terms of different number of tables mapped to different number of classes.

Identity and Equality

The notion of identity is driven by primary key in relational model. Given a primary key you will always retrieve the same data. Also it does not matter, how many clients are retrieving the same data. With right isolation level all will see the same data. In Java the situation becomes more complex. The same data row might be represented by more objects in Java layer. For example a User data in database with primary key 1 might have more than one object in different thread. The question comes which is the object having the right data. Also if all thread try to save the same object, than who wins? Similar problem arises related to equality. In Java the default is reference equality. If the references are pointing to the same object than they are equal. The corollary is that if there are two objects representing the same data row, they will come out as different. To solve this we have to give implementation to the equals methos, but it's not always trivial. ORM solutions has to provide provisions to maintain the correct notion of equality and identity. The frameworks usually ask to explicitly map the primary key to an attribute.

Association

In Java the relationship is done by association. For example User has a reference of address. However in Tables, the relationship is done with foreign key association. Java has also the notion of directionality. For example you can access address form User but not the other way round. To build the relationship from both side you have to put the reference on both the sides.
User
public class User{ private Address address; //Setters and Getters }

Address
public class Address{ private User user; //setters and getters }

However there is no notion of directionality in the relational world.The foreign key relationship can be done at any one end and the relationship is build. With SQL you can navigate from any end. For example the foreign key relationship build at User side will be
Table ADDRESS
ADDRESS_ID CITY COUNTRY

Table USER
USER_ID NAME ADDRESS_ID (Fk)


ORM solutions has to deal with these association while setting and getting the data from the database.

Type Systems

Object Oriented and relational world has different type systems. For example in relational world string can be constrained on the size however on the Java side you can point the reference of String to any size based on memory allocated. Also date and times are handled differently in Java and relational world. For example in some databases there is a distinction between date, time and timestamp. Time stamp contains both date and time. In Java, the date is a long value which contains the date and time both. So when you fetch the date or time from relational world how you map it to a Java data type of Date or Calendar.

Databases are Different

There are so many popular databases in the market. In spite of SQL being a standard, there are still variations in the SQL support in each database and there are many vendor extensions and individual flavors in each database. Though this is not really an ORM issue but it is important from the perspective of database portability, if you are looking for it. Ensuing database portability using JDBC is usually a hurricane task if you wish to use the individual flavors of database for performance or other reasons. ORM frameworks attempt to handle most of the important databases transparently. Also they do have extension mechanisms if you wish to support another database

A video explaining the above concepts:




ORM frameworks adopt different strategies to solve these kind of mismatches. ORM frameworks strive to preserver the notion of object world concepts and shield the developers from relational world by taking care of mappings. This should be taken as an excuse to not to learn relational concepts. In fact on the other way to be a good user of ORM frameworks, one should understand how the mapping works and the implications of it. The number issue in using ORM frameworks is performance and most of the time its because of not understanding how the ORM frameworks map to the relational world and not having a good grasp of relational world concepts.


ORM Framework Index  Back To Java Home  Back To Home


HaHA! I have already seen the great thing!!!



Enjoyed every bit of your blog.Thanks Again. Great.



good article, I wait for the next article .thank you...



There are so many in different shops.i can look one by one.if i found url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-c-7.html(external link)ugg classic cardy boots/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-5825-sand-p-415.html(external link)ugg classic short sand/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-5825-chestnut-p-411.html(external link)ugg classic short chestnut/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-5815-chestnut-p-387.html(external link)ugg classic tall chestnut/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-5815-black-p-386.html(external link)ugg classic tall black/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-5815-chocolate-p-388.html(external link)ugg classic tall chocolate/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-5815-sand-p-390.html(external link)ugg classic tall sand/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-oatmeal-p-401.html(external link)ugg classic cardy oatmeal/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-grey-p-400.html(external link)ugg classic cardy boots grey/url i love.i'll by url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-black-p-398.html(external link)ugg classic cardy black/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-grey-p-400.html(external link)ugg classic cardy grey/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-c-7.html(external link)ugg classic cardy sale/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-5825-black-p-410.html(external link)ugg classic short black/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-5825-grey-p-413.html(external link)ugg classic short grey/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-5825-chocolate-p-412.html(external link)ugg classic short chocolate/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-boots-c-104.html(external link)ugg classic short sale/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-boots-c-104.html(external link)women's ugg classic short boots/url. I like shopping on weekdays when of shopping mall are empty, like telling myself url=http://www.buydiscountuggs.com/(external link)ugg/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots/url ,url=http://www.buydiscountuggs.com/(external link)uggs/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots sale/url ,url=http://www.buydiscountuggs.com/(external link)uk ugg boots/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots cheap/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots buy/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-c-7.html(external link)ugg cardy/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-boots-c-103.html(external link)ugg classic tall/url ,url=http://www.buydiscountuggs.com/ugg-classic-short-boots-c-104.html(external link)ugg classic short/url ,url=http://www.buydiscountuggs.com/(external link)uggs boots/url ,url=http://www.buydiscountuggs.com/(external link)ugg store/url, I'm so free. I don't care url=http://www.buydiscountuggs.com/ugg-flip-flops-c-100.html(external link)ugg flip flops/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots discount/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots online/url ,url=http://www.buydiscountuggs.com/(external link)ugg boots store/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-c-7.html(external link)ugg cardy boots/url ,url=http://www.buydiscountuggs.com/ugg-bailey-button-boots-c-101.html(external link)ugg baileys boot/url ,url=http://www.buydiscountuggs.com/ugg-bailey-button-boots-c-101.html(external link)ugg bailey boot/url ,url=http://www.buydiscountuggs.com/ugg-classic-knit-boots-ugg-classic-argyle-knit-5879-c-102_16.html(external link)ugg argyle boots/url ,url=http://www.buydiscountuggs.com/ugg-bailey-button-boots-c-101.html(external link)ugg bailey boots/url ,url=http://www.buydiscountuggs.com/(external link)ugg boot discount price/url in which shop.With only two weeks to url=http://www.fivefingersshoesvibram.com/(external link)vibram five fingers/url , buying url=http://www.buydiscountuggs.com/(external link)ugg australia/url ,url=http://www.buydiscountuggs.com/(external link)ugg sale/url ,url=http://www.buydiscountuggs.com/(external link)uggs on sale/url ,url=http://www.buydiscountuggs.com/(external link)ugg mall/url ,url=http://www.buydiscountuggs.com/ugg-classic-cardy-5819-c-7.html(external link)ugg classic cardy/url , url=http://www.buydiscountuggs.com/ugg-classic-short-boots-c-104.html(external link)ugg classic short boots/url ,url=http://www.buydiscountuggs.com/ugg-classic-tall-boots-c-103.html(external link)ugg classic tall boots/url ,url=http://www.buydiscountuggs.com/ugg-classic-mini-5854-c-9.html(external link)ugg classic mini/url ,url=http://www.buydiscountuggs.com/(external link)ugg australia deckers/url ,url=http://www.buydiscountuggs.com/(external link)ugg australia sale uk/url is a high priority for a lot of people. However, this year not so many people are leaving their homes to browse around url=http://www.buydiscountuggs.com/ugg-classic-tall-boots-c-103.html(external link)ugg australia classic tall/url ,url=http://www.buydiscountuggs.com/ugg-bailey-button-boots-c-101.html(external link)ugg bailey button/url ,url=http://www.buydiscountuggs.com/ugg-bailey-button-boots-c-101.html(external link)ugg bailey button boots/url ,url=http://www.buydiscountuggs.com/(external link)ugg deckers/url ,url=http://www.buydiscountuggs.com/(external link)ugg dealers/url ,url=http://www.buydiscountuggs.com/(external link)ugg uk store/url ,url=http://www.buydiscountuggs.com/(external link)ugg uk online/url ,url=http://www.buydiscountuggs.com/(external link)ugg uk sale/url ,url=http://www.buydiscountuggs.com/ugg-classic-knit-boots-c-102.html(external link)ugg knit boots/url ,url=http://www.buydiscountuggs.com/ugg-classic-knit-boots-c-102.html(external link)ugg knitted/url.



There are so many in different shops.i can look one by one.if i found ugg classic cardy boots ,ugg classic short sand ,ugg classic short chestnut ,ugg classic tall chestnut ,ugg classic tall black ,ugg classic tall chocolate ,ugg classic tall sand ,ugg classic cardy oatmeal ,ugg classic cardy boots grey i love.i'll by ugg classic cardy black ,ugg classic cardy grey ,ugg classic cardy sale ,ugg classic short black ,ugg classic short grey ,ugg classic short chocolate ,ugg classic short sale ,women's ugg classic short boots. I like shopping on weekdays when of shopping mall are empty, like telling myself ugg ,ugg boots ,uggs ,ugg boots sale ,uk ugg boots ,ugg boots cheap ,ugg boots buy ,ugg cardy ,ugg classic tall ,ugg classic short ,uggs boots ,ugg store, I'm so free. I don't care ugg flip flops ,ugg boots discount ,ugg boots online ,ugg boots store ,ugg cardy boots ,ugg baileys boot ,ugg bailey boot ,ugg argyle boots ,ugg bailey boots ,ugg boot discount price in which shop.With only two weeks to vibram five fingers , buying ugg australia ,ugg sale ,uggs on sale ,ugg mall ,ugg classic cardy , ugg classic short boots ,ugg classic tall boots ,ugg classic mini ,ugg australia deckers ,ugg australia sale uk is a high priority for a lot of people. However, this year not so many people are leaving their homes to browse around ugg australia classic tall ,ugg bailey button ,ugg bailey button boots ,ugg deckers ,ugg dealers ,ugg uk store ,ugg uk online ,ugg uk sale ,ugg knit boots ,ugg knitted.


Post new comment

Click for Help
BoldItalicUnderlineStrikethroughExternal LinkSmileys
Anti-Bot verification code: Random Image
Post new comment