Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Wednesday, January 20, 2010

An Example for "dirty checking" in Hibernate

As I have written about the features of hibernate in the previous post.One of them was dirty checking by hibernate.
Now what does dirty checking actually means?To see this practically lets look at the following class with name Computer.

public class Computer
{
private String name;
private Double prize;
public Computer()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Double getPrize()
{
return prize;
}
public void setPrize(Double prize)
{
this.prize = prize;
}
}
Here I have prize and name as class attributes and I have setter and getter for them.Also I make the name attribute as the primary key in the .hbm.xml file.
Now lets have a class(say Manager) which deals with the retrieval of the row of table Computer,which looks as given bellow


public class Manager
{
public static void main(String[] args)
{
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Computer c = (Computer)session.load(Computer.class, new String("Mac"));
System.out.println(c.getName());
System.out.println(c.getPrize());
c.setPrize(111111);=========>imp. step
session.getTransaction().commit();
session.flush();
session.close();
}
}
Now here when I make the prize of the computer as 111111 in the imp. step, and after the execution of Manager class gets over and I go to the DB and see that the value of the prize has been changed to 111111.
Now the point to be noted is that we changed prize to a new value and did not do a explicit save after changing the value(by calling session.save(c)),but still the changes are made.This auto-updating of DB with reference to the change in the object is called "dirty checking" in terms of Hibernate.

Tuesday, October 13, 2009

Why to use hibernate when other persistence mechanism are there??

Well there are many reasons for hibernate to be used in place of others.

I list some of the most important reasons.

1:)Hibernate provides the Object Relation Mapping(ORM):
Well it has always been a concern for the java developers,they wanted a tool that can provide a proper ORM.Through ORM,the classes(POJO) are directly mapped to the tables and the object state is persisted as the row in that table.

2:)Hibernate provides Transparent and Automated persistence:
Transparent means that hibernate allows the class (that is to be persisted) to be free from any special implementation.Meaning that you don't have to implement any interface or extends to any class.Hence the developers don't have to consider anything about the persistence.

Automated is something that allows the programmers to be free from the low level database implements.Hence developers don't have to write low level queries,they are generated by hibernate(it uses its own query language called HQL).

3:)Hibernate supports Dirty Checking:
This makes sure that any changes that are made to the object will also be reflected in the database.Hence you don't have to again explicitly save the object.

4:)Hibernate support Fine-grained domain models:
This is the most important requirement for rich domain model.This basically allows you to have more classes than the tables.

Example in a design for a User entity we may have address,street no,city etc. as attributes,but wait there is a problem with this:what if a User has a home address,office address.Well you can have soo maany columns for each of them.

But in hibernate you can have a separate class called Address with all of the above as attributes.This basically reduces the time taken to execute the queries.

5:)Hibernate also supports the dynamic manipulation of meta-data:
Using this you can update the database schema even at the run time.

6:)Hibernate offers platform independence:
With this you can even work on legacy systems with out any problems.

7:)Hibernate supports polymorphic queries:
Well this feature of Hibernate is quite unique.This basically allows to retrieve the DB row and store it in super class reference.

Well the list continues.I must say that its really worth using Hibernate in application development.But its always better to use it in big application.