Pradeep's Blog

Google

Sunday, March 26, 2006

Need for an ORM package like Hibernate?

Relational database management systems are proven data management technology and are almost always a requirement in any Java project. While using objects in our application, we sometimes reach the point where we want them to persist using a RDBMS. We would typically open a JDBC connection, create an SQL statement and copy all our objects property values over.

Since Java does not have SQL type, we are forced to use String concatenation which errors prone and time consuming. This may be easy for objects with small set of properties, but for an object with many properties just preparing the SQL statement would itself be complicated.

That’s not the only problem, what about things like associations, foreign key constraints? And situations like adding a new property to an object half way through the development, trying to use the application with other database; do we go and change all associated SQL statements in the code to suit our new needs? Remember that the problem applies to both loading properties from the database into object’s properties and Vis-versa.

So what can Hibernate do for us? Hibernate basically intends to takes most of that burden of our shoulder. By using Hibernate we only have to define the way we map our classes to tables once - which property maps to which column, which class to which table, etc. After this, we should be able to do things like this:

session.save(MyObj);//statement 1
myObject = session.load(MyObject.class, objectId);//statement 2


The first statement saves the objects properties into the appropriate column in the database table & the second statement the opposite as per our mapping.

Hibernate will automatically generate all the SQL needed to store the object. It relieves us of manual JDBC result set handling, objects conversion, and keeps our application portable to all SQL databases, leaving us to concentrate on the business logic alone. Hence Hibernate helps in rapid product development, minimises maintenance & development costs.
References:

0 Comments:

Post a Comment

<< Home