Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Hibernate 3.

What is Hibernate
Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it seem as if your database contains plain Java objects like you use every day, without having to worry about how to get them out of (or back into) mysterious database tables.

Hibernate liberates you to focus on the objects and features of your application, without having to worry about how to store them or find them later.

Why Hibernate?
Cost effective. Learning is very easy compare to EJB.

High Performance than EJB


No more need for JDBC API for Result set handling. Switching to other SQL database requires few changes in Hibernate configuration file

Advantage of using the Hibernate


Database independent application Avoid writing queries Avoid JDBC API completely Hibernate uses connection pooling technique

Automatic Key Generation


Develop the Application in short Period of time

Hibernate Support Different Database


DB2 MySQL PostgreSQL Oracle (any version) Microsoft SQL Server HypersonicSQL Informix Ingres Interbase Pointbase Mckoi SQL Progress FrontBase SAP DB Sybase

Hibernate Architecture

Hibernate Architecture
Hibernate architecture has three main components: Connection Management Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.

Transaction management: Transaction management service provide the ability to the user to execute more than one database statements at a time.
Object relational mapping: Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query.

Hibernate is very good tool as far as object relational mapping is concern but in terms of
connection management and transaction management, it is lacking in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the Hibernate.

Hibernate 3.0
Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language Hibernate Criteria Query API Hibernate Native Query

Hibernate Criteria Query API


The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective.

Hibernate Native Query


Native SQL is handwritten SQL for all database operations like create, update, delete and select. Hibernate Native Query also supports stored procedures. Hibernate allows you to run Native SQL Query for all the database operations, so you can use your existing handwritten sql with Hibernate, this also helps you in migrating your SQL/JDBC based application to Hibernate.

How to Implement Hibernate


Configuring Hibernate Persistence Class Map the Object to the Database table Setting up the Database
Insert, Update and Delete records in the table (Hibernate automatically creates query to perform this operations)

1. Configuring Hibernate
Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment. <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> </session-factory> </hibernate-configuration>

2. Persistence Class
Hibernate uses the Plain Old Java Objects (POJO) classes to map to the database table. We can configure the variables to map to the database column.

package Example;
public class Contact { private String firstName; private String lastName; private String email; private long id; public String getEmail() { return email; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setEmail(String string) { email = string; } public void setFirstName(String string) { firstName = string; } public void setLastName(String string) { lastName = string; } public long getId() { return id; } public void setId(long l) { id = l; } }

3.Map the Object to the Database table


The file contact.hbm.xml is used to map Contact Object to the Contact table in the database. <hibernate-mapping> <class name=Example.Contact" table="CONTACT"> <id name="id" type="long" column="ID" > <generator class="assigned"/> </id>

<property name="firstName"> <column name="FIRSTNAME" /> </property> <property name="lastName"> <column name="LASTNAME"/> </property> <property name="email"> <column name="EMAIL"/> </property> </class> </hibernate-mapping>

4.Setting up the Database


package Example; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class FirstExample { public static void main(String[] args) { Session session = null; try{ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); values in it by reading them from form object Contact contact = new Contact(); contact.setId(3); contact.setFirstName(kumar"); contact.setLastName(m"); contact.setEmail(kumar.m@yahoo.com"); session.save(contact); System.out.println("Done"); }catch(Exception e){ System.out.println(e.getMessage()); }finally{ session.flush(); session.close(); } } }

Thank you

You might also like