Monday, October 1, 2012

What is the need of ORM framework like Hibernate ?

There are two models :
  • Object model
  • Relational model
Object model :
  • Object model is nothing but Object Oriented System.
  • In Object model system, objects are the components.
  • Objects are instances of classes.
  • Classes are related to other classes via inheritance relationships.
  • An Object model helps to create reusable application frameworks.
  • An Object has an identity, a state and behavior.
  • An Object model uses the principles of abstraction, encapsulation, polymorphism, modularity and typing.
Relational model :
  • A Relational model deals with Database.
  • Defines the structure of datadata manipulation, and data integrity.
  • Data is organized in the form of tables.
  • Different tables are associated by means of referential integrity ( a foreign key).
  • Integrity constraints such as primary key, unique check constraints, and not null are used to maintain an entity's integrity in the Relation model.

Mismatch or Difference between Object model and Relational model :
  • In an Object model, you use the state of the model to define equality between objects. But in aRelational model, you use primary key to define equality of entities.
  • Object references are used to associate different objects in an object model, whereas aforeign key is used to establish associations in a Relational model.
Because these two models are distinctly different, you need a way to persist object entities (Java objects) into a relational database.

Why do we need ORM (Object Relational Mapping) framework ?

Without ORM framework :
  • Need to interact with Database with direct SQL queries.
  • From an application developer point of view, it is very hard to write hundreds of lines of code in order to deal with Database for persisting data / fetching data or any DML operations to do.
  • In an Object Graph, let's say if there a number of objects associated with each other. So, as a developer after fetching data from multiple tables, we need to build result data into appropriate objects by iterating over ResultSet. It is a repetitive task every time dealing with database.  It is a headache to the developer.
With ORM framework :
    • In ORM framework, we think database tables as classes and each row as an object.
    • We don't interact with Database directly with straight forward SQL queries.
    • We think database interms of Classes and Objects only not tables and rows.
    • For example, If you want to save Employee object data. We instruct to ORM framework like hibernate to save Employee object by indicating some meta data about in which table data should be saved and which fields should be saved in which columns.

    Advantages of ORM framework :
    • Productivity : development time should be reduced.
    Disadvantages:
    • Performance : poor performance if huge amount of data exists. 

    Hibernate is one of the popular ORM framework.

    Sunday, September 30, 2012

    How to download and Install Hibernate framework?


    Any framework software is not an installation software, i mean not an executable software.
    A framework is nothing but a collection of class files bundled in the form of JAR files.

    Hibernate framework is a set of JAR files. Working with hibernate means, just adding those hibernate jar files to the application.

    Download Hibernate framework software from the URL http://sourceforge.net/projects/hibernate/files/hibernate3/
    • Choose hibernate version 3.2.2-ga.zip ,which is a standard version.
    • After downloaded, Unzip it.
    • Find Hibernate API document in the /doc/api folder.
    • In lib directory, you'll find various jar files.

    Required JAR files:
    • antlr-2.7.6.jar
    • asm.jar
    • asm-attrs.jar
    • cglib-2.1.3.jar
    • commons-collections-2.1.1.jar
    • commons-logging-1.0.4.jar
    • ehcash.jar
    • dom4j-1.6.1.jar
    • hibernate3.jar
    • jta.jar
    • log4j-1.2.3.jar

      NOTE: For annotations some other jar files need to be added. When time comes i'll tell those jar files.

      Saturday, September 29, 2012

      How to use @Required Annotation in Spring?


      In most cases, you would like to check if particular properties have been set, but not all properties of certain types. For this Spring provides @Required annotation in order to check a particular property. It works in Java1.5 and above versions only.
      Let's use @Required annotation in  application.AccountService.java

      1. package org.myjavaswtech.service;  
      2.   
      3. import org.myjavaswtech.dao.AccountDAO;  
      4. import org.springframework.beans.factory.annotation.Required;  
      5.   
      6. public class AccountService {  
      7.   
      8.  private AccountDAO accountDao;  
      9.   
      10.  @Required  
      11.  public void setAccountDao(AccountDAO accountDao) {  
      12.   this.accountDao = accountDao;  
      13.  }   
      14.  public void transferFund(String acNo, String benAcNo, double amount){  
      15.   System.out.println("Call to Account DAO");  
      16.   accountDao.transferFund(acNo, benAcNo, amount);  
      17.  }  
      18. }  

      Inorder to work @Required Annotation, you need to register RequiredAnnotationBeanPostProcessor instance in the IoC container.

      RequiredAnnotationBeanPostProcessor is a Spring bean post processor that checks if all the bean properties with the @Required annotation have been set.

      beans.xml
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans xmlns="http://www.springframework.org/schema/beans"  
      3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      4.  xsi:schemaLocation="http://www.springframework.org/schema/beans   
      5.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
      6.  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
      7.    
      8.  <bean name="accountDao" class="org.myjavaswtech.dao.AccountDAO">  
      9.  </bean>  
      10.   
      11.  <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">  
      12.   <!-- <property name="accountDao" ref="accountDao" /> -->  
      13.  </bean>  
      14.   
      15.  <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >  
      16.   <property name="senderAcNo" value="ac123456"/>  
      17.   <property name="beneficiaryAcNo" value="ac7890" />  
      18.   <property name="amount" value="10000"/>  
      19.   <property name="accountService" ref="accountService"/>  
      20.  </bean>  
      21.   
      22. </beans>  
      Now Run the application: You'll get the following exception
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'accountDao' is required for bean 'accountService'.

      Spring2.5 and Above : <context:annotation-config>
      In Spring2.5 and above versions, <context:annotation-config> is used in IOC container instead of RequiredAnnotationBeanPostProcessor. If you use <context:annotation-config> element in your bean configuration file, and a RequiredAnnotationBeanPostProcessor instance will automatically get registered.

      beans.xml
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans xmlns="http://www.springframework.org/schema/beans"  
      3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      4.  xmlns:context="http://www.springframework.org/schema/context"  
      5.  xsi:schemaLocation="http://www.springframework.org/schema/beans   
      6.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
      7.  http://www.springframework.org/schema/context  
      8.  http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
      9.    
      10.  <context:annotation-config/>  
      11.    
      12.  <!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->  
      13.    
      14.  <bean name="accountDao" class="org.myjavaswtech.dao.AccountDAO">  
      15.  </bean>  
      16.   
      17.  <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">  
      18.   <!-- <property name="accountDao" ref="accountDao" /> -->  
      19.  </bean>  
      20.   
      21.  <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >  
      22.   <property name="senderAcNo" value="ac123456"/>  
      23.   <property name="beneficiaryAcNo" value="ac7890" />  
      24.   <property name="amount" value="10000"/>  
      25.   <property name="accountService" ref="accountService"/>  
      26.  </bean>  
      27.   
      28. </beans>  
      In the above configuration we used context namespace and schema location.

      Now Run the application: You'll get the following exception
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'accountDao' is required for bean 'accountService'.