Wednesday, June 13, 2012

Hibernate


1. ORM tool (Object-Relational Model)
2. used in data layer of applications
3. implements JPA (Java persistent API)

Hibernate to solve the problem of
1. mapping member variables to columns, mapping relationships (foreign key , primary key)
2. handling data types
3. managing changes to object state


Primary use to bridge the gap between objects and relational database (tables).


Setting hibernate with eclipse:

1. Go to www.hibernate.org/downloads and download the latest version from release bundle.
(I used 4.1.4.Final).
2.  required folder in lib contains all the essential jar files.
3. Create new java project as the first hibernate project (HbProject). Go to properties->java build path ->libraries tab -> create new user library (name it as hibernate)
4. add all the jar files from the required folder in lib (refer the downloaded release bundle)
5. add the newly created library to HbProject.


Choosing database:

I am using MySQL database. (can change the settings according to db). Now will need a jbdc-mysql driver. Download the driver from http://dev.mysql.com/downloads/connector/odbc/ .

Add the downloaded jar to the build path of HbProject. So go to properties->java build path -> add external jars -> mysql-connector-java-bin.jar


To use another database install respective jdbc driver for that database.


Steps needed to save object without hibernate:

1. jdbc database config
2. create objects and design database
3. Data access object layer in order save the object using sql queries

With Hibernate

1. jdbc database config- hibernate config
2. Annotations (in order to model the object)
3. service method to create the model object - hibernate api

Best part is you don't need the database design and data access object later ( need not to write SQL queries) !!!!!!!!



Writing configuration file

Create xml file named hibernate.cfg.xml. This is default name for hibernate config file. (use different name, pass it to configuration, so hibernate knows).



Required settings 


Database connection settings:
 1. driver class
  you can find the property com.mysql.jdbc.Driver in your mysql driver. Expand it. you will find driver class. It's in package com.mysql.jdbc and so the property is com.mysql.jdbc.Driver.

 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

2. connection url

Here localhost is because mysql is installed in local machine and default port number is 3306. hibernatedb is the database which is used in order to save the objects in tables.

Also create hibernatedb in mysql
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>



3. username and password
Set the properties according to the settings (no password to root so no value)
<property name="connection.username">root</property>
        <property name="connection.password"></property>



4. connection pool size and dialect

You can find dialect property in hibernate-core-4.1.4.Final.jar -> org.hibernate.dialect -> MySQLDialect. (Dialect used to let hibernate know that which language should be used in order to talk to db)

You can change this property according to the db.


 <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>



5.  To see which SQL command has been executed by hibernate

          <property name="show_sql">true</property>




6. Mapping class 


You also have mapping class. (contains the class name whose object we are mapping to sql)


        <mapping class="org.kanchan.dto.UserDetails"/> 


7. hbm2ddl.auto



<property name ="hbm2ddl.auto">create</property>



create: drop and recreate the database again 
update: will update the existing table/db

No comments:

Post a Comment