Monday, April 26, 2010

Some Point Related To Hibernate

Hi !

Over here I am going to discuss some point and concept discussed in the Chapter# 4 of "Hibernate In Action By Christian Bauer, Gavin King".

  • Concept of Value Type and Entity Type

    Value Type and Entity Type both are java objects(or POJOs) but Hibernate do conceptual differentiation between them and also provide a syntax and guidelines to implement it.

  • Concept:
    • Entity Type objects have their own database identity(primary key) where as Value Type does not have.

    • Entity Type objects are persisted as a separate row in the database, whereas Value Type objects are persisted as part of the row.

    • Life span of Value Type is depend on it Owning Entity.
  • Concept of Fine-grain domain models

    Hibernate gives the support for fine grained object models which implies that "more objects than tables". Fine grain classes helps in implementing type safety and behaviors.

To understand the above concept lets creates a simple example
  • Create the table

    create table USERS (
    USERNAME varchar(15) not null primary key,
    NAME varchar(50) not null, HOME_ADDRESS varchar(100),
    OFFICE_ADDRESS varchar(100),
    EMAIL varchar(45)
    )

    insert into users(username, name, home_address, office_address, email) values("Rehan","Rehan","Rehan Home Address","Rehan Office
    Address","rehan@emailaddress.com");

    insert into users(username, name, home_address, office_address, email) values("Huzefa","Huzefa","Huzefa Home Address","Huzefa Office
    Address","huzefa@email.com");

  • Create a Java Project in Eclipse or IDE of your choice and configure it for hibernate implementation (I will not go into details of it that how it will be done).
    What I have done is
    created a package "entities" and put the following classes and hbm file in it.

    Users.java

    package entities;

    public class Users {

    private String userName;
    private String name;
    private Address homeAddress;
    private Address officeAddress;
    private EmailAddress email;

    // Getters and Setters

    } // Useres

    Users.hbm.xml

    <?xml version="1.0"?>

    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


    <hibernate-mapping>

    <class name="entities.Users" table="users">
    <id name="userName" type="java.lang.String"
    column="username">
    <generator class="native"/>
    </id>

    <property name="name" column="name" type="java.lang.String"
    />

    <component name="homeAddress" class="entities.Address">
    <property name="address" type="java.lang.String" column="home_address" />

    </component>
    <component name="officeAddress" class="entities.Address">
    <property name="address" type="java.lang.String" column="office_address"
    />
    </component>

    <component name="email" class="entities.EmailAddress">
    <property name="email" type="java.lang.String" column="email" />
    </component>

    </class>

    </hibernate-mapping>



    Address.java


    package entities;


    public class Address {

    private String address;

    // Getters and Setters

    }

    EmailAddress.java

    package entities;

    public class EmailAddress {

    private String email;

    // Getters and Setters

    }

    ValueTypeTest.java

    package test;

    import org.hibernate.Session;
    import entities.Users;
    import util.HibernateUtil;

    public class ValueTypeTest {

    public static void main(String[] args) {

    try{
    try{
    Session session= HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Users user= (Users)session.get(Users.class, "Rehan");
    System.out.println("UserName= " +user.getUserName());
    System.out.println("Name= " +user.getName());
    System.out.println("Home Address= " +user.getHomeAddress().getAddress());
    System.out.println("Office Address= " +user.getOfficeAddress().getAddress());
    System.out.println("Email= " +user.getEmail().getEmail());
    session.close();
    }finally{
    HibernateUtil.shutdown();
    }
    }catch(Exception ex){
    ex.printStackTrace();
    }
    } // main

    } // class ValueTypeTest

In the above example User is Owning Entity and Address and Email are value type objects.