Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to...

17
Hibernate 3.0

Transcript of Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to...

Page 1: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

Hibernate 3.0

Page 2: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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.

Page 3: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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

Page 4: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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

Page 5: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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

Page 6: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

Hibernate Architecture

Page 7: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

Hibernate Architecture

Hibernate architecture has three main components:

Connection ManagementHibernate 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.

Page 8: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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.

Page 9: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

Hibernate 3.0

Hibernate 3.0 provides three full-featured query facilities:

Hibernate Query LanguageHibernate Criteria Query APIHibernate Native Query

Page 10: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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. 

Page 11: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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.

Page 12: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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)

Page 13: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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>

Page 14: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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; }}

Page 15: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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>

Page 16: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

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(“[email protected]");        session.save(contact);        System.out.println("Done");    }catch(Exception e){      System.out.println(e.getMessage());    }finally{      session.flush();      session.close();

 }   } }

Page 17: Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.

Thank you