Hibernate 3.0 - PowerPoint PPT Presentation

About This Presentation
Title:

Hibernate 3.0

Description:

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 seem as if ... – PowerPoint PPT presentation

Number of Views:484
Avg rating:3.0/5.0
Slides: 18
Provided by: bci9
Category:

less

Transcript and Presenter's Notes

Title: Hibernate 3.0


1
Hibernate 3.0
2
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.

3
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

4
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

5
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

6
Hibernate Architecture
7
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.

8
  • 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.

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

10
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. 

11
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.

12
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)

13
1. Configuring Hibernate
  • Hibernate uses the hibernate.cfg.xml to create
    the connection pool and setup required
    environment.
  • lthibernate-configurationgtltsession-factorygt   ltpr
    operty name"hibernate.connection.driver_class"gtco
    m.mysql.jdbc.Driver
  • lt/propertygt   ltproperty
    name"hibernate.connection.url"gtjdbcmysql//local
    host/hibernate
  • lt/propertygt      ltproperty name"hibernate.conne
    ction.username"gtrootlt/propertygt      ltproperty
    name"hibernate.connection.password"gtlt/propertygt 
         ltproperty name"hibernate.connection.pool_siz
    e"gt10lt/propertygt      ltproperty
    name"dialect"gtorg.hibernate.dialect.MySQLDialectlt
    /propertygt      ltproperty name"hibernate.hbm2ddl
    .auto"gtupdatelt/propertygt      lt!-- Mapping files
    --gt      ltmapping resource"contact.hbm.xml"/gtlt/
    session-factorygt
  • lt/hibernate-configurationgt

14
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 getFirstN
    ame()     return firstName
         public String getLastName()     return l
    astName     public void setEmail(String
     string)     email  string
        public void setFirstName(String string)  
       firstName  string     public void se
    tLastName(String string)     lastName  string
        public long getId()     return id
        public void setId(long l)     id  l

15
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.
  • lthibernate-mappinggt  ltclass nameExample.Contact
    " table"CONTACT"gt   ltid name"id" type"long"
    column"ID" gt   ltgenerator class"assigned"/gt 
    lt/idgt  ltproperty name"firstName"gt    
    ltcolumn name"FIRSTNAME" /gt  lt/propertygt 
    ltproperty name"lastName"gt    ltcolumn
    name"LASTNAME"/gt  lt/propertygt  ltproperty
    name"email"gt    ltcolumn name"EMAIL"/gt 
    lt/propertygt lt/classgt
  • lt/hibernate-mappinggt

16
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()       val
    ues in it by reading them from form object       
    Contact contact  new Contact()        contact.s
    etId(3)        contact.setFirstName(kumar")  
          contact.setLastName(m")        contact.se
    tEmail(kumar.m_at_yahoo.com")        session.save(
    contact)        System.out.println("Done")    
    catch(Exception e)      System.out.println(e.ge
    tMessage())    finally      session.flush()
          session.close()
  •     

17
Thank you
Write a Comment
User Comments (0)
About PowerShow.com