SlideShare a Scribd company logo
Hibernate

     Introduction and Overview
          >By Manisha


                       May 2012
Agenda
    At the end of this course, you should be able to:
     Describe the basics of Hibernate
     Use Hibernate configuration
     Explain basic O/R Mapping
     Write simple application in Hibernate

    ●
           Q&A




2   Hibernate Basic                      May 2012
Hibernate

    ●
        ORM technology
     Consistent database communication and Increased maintainability: In
      Hibernate database communication policies are applied as configuration,
      in contrast as to code them in SQL based communication.
     Developers with limited SQL knowledge can participate in development
     Reduced testing time
     Performance Optimization: Fine tuning database communication,
      adopting caching strategies can be done with configuration.
     Portable code across multiple databases: Hibernate API and HQL can
      facilitate database neutral programming.




3        Hibernate Basic                         May 2012
What is ORM?

      Object Relational Mapping (ORM) frameworks are those which
       encapsulate SQL communication with relational databases, with object-
       oriented communication.

      ORM is a programming way for converting data between incompatible
       type systems in relational databases and object-oriented programming
       languages.

      Currently, several ORM frameworks such as Entity beans in Enterprise
       JavaBeans (EJB), TopLink from Oracle, iBatis, Hibernate, and so on are
       available.




4       Hibernate Basic                             May 2012
Difference in conventional and ORM based implementation


     Conventional Implementation


          Service                     Data
                                     Access                 Database
          Object
                                     Object




     Implementation with ORM


                                    Data
          Service                             Hibernate
                                   Access
          Object                                            Database
                                   Object




5    Hibernate Basic                             May 2012
High level Hibernate
    Architecture


                                        Application

                                      Persistent Objects



                                         Hibernate
                      Configuration                           Mapping



                                         Database




6   Hibernate Basic                                    May 2012
Basic content of Hibernate application



    In any standard hibernate application will have below data
     Pojo class of table
     Configuration files(hibernate.cfg.xml)
     Mapping file(xxx.hbm.xml)
     Application code to create Hibernate Runtime and execute




7        Hibernate Basic                             May 2012
How to use Hibernate for data communication?



    Follow these steps to use Hibernate for data communication:
     Prepare domain objects (POJO) for table.
     Write mapping information.
     Write Hibernate configuration.
     Create Hibernate Runtime.
     Use Hibernate Runtime to communicate with database.




8        Hibernate Basic                           May 2012
Hibernate Example: Prepare pojo object(Class to table mapping)

    package com.ibm.tf.hibernate;
    public class Employee implements Serializable{

    // attributes - relate to business entities
    private Integer id;
    private firstName;
    private lastName;
    // accessor and mutator methods
    public Integer getId() {
    //implementation code
    }
    ….
    public void printEmployeeName() {
    // some business method
    }
    }
9         Hibernate Basic                            May 2012
Write mapping information(Table mapping to conf. Xml file)
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
 <hibernate-mapping>
 <!– Employee class mapping to EMPLOYEE table-->
   <class name=" com.ibm.tf.hibernate.Employee“ table=“EMPLOYEE">
          <!– identifier column mapping-->
          <id name="id“ column=“ID“ type="long">
                   <generator class="native"/>
          </id>
          <!– property mapping--->
          <property name=“firstName“ column=“FIRST_NAME“ type="string"/>
          <property name=“lastName“ column=“LAST_NAME“ type="string"/>
   </class>
 </hibernate-mapping>

10      Hibernate Basic                            May 2012
Write Hibernate configuration information
 Hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
    <session-factory>
      <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.url">jdbc:hsqldb:file:testdb</property>

     <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>

     <property name="current_session_context_class">thread</property>
     <property name="show_sql">true</property>
     <property name="hbm2ddl.auto">update</property>

     <mapping resource="com/hibernate/test/Employee.hbm.xml"/>
   </session-factory>
 </hibernate-configuration>



11       Hibernate Basic                                     May 2012
Write Hibernate configuration information

The following are illustrative (not exhaustive) configuration parameters sorted by their
  categories. With category description we can guess on what kind of values could be
  assigned to individual parameters.
 Hibernate JDBC Properties (useful for standalone)
     – hibernate.connection.driver_class
     – hibernate.connection.url
     – hibernate.connection.username
     – hibernate.connection.password
 Hibernate Datasource Properties (useful inside container)
     – hibernate.connection.datasource
     – hibernate.jndi.url
 List of managed entities
 Hibernate Configuration Properties:
     – hibernate.dialect n hibernate.order_updates

12      Hibernate Basic                              May 2012
Create Hibernate runtime
     Before creating hibernate runtime, lets understand It
      Hibernate Session, Transaction, and all Objects working behind them are
       collectively called as Hibernate Runtime.
      Information like how to connect to database, how to map entity data to table
       row, how to handle transactions and many more are provided to Hibernate
       Runtime by means of configuration.
      As Hibernate is designed to operate in many different environments. Hence,
       there are:
          – Many configurable parameters of various category.
          – Many ways to configure Hibernate Runtime.




13        Hibernate Basic                              May 2012
Create hibernate runtime

     public class HibernateUtil {
     private static final SessionFactory sessionFactory;
     static {
     try {// Create the SessionFactory from hibernate.cfg.xml
     sessionFactory = new Configuration().configure()
     .buildSessionFactory();
     } catch (Throwable ex) {
     throw new ExceptionInInitializerError(ex);
     }
     }
     public static SessionFactory getSessionFactory() {
     return sessionFactory;
     }
     }

14         Hibernate Basic                                      May 2012
Use Hibernate Runtime
     public class EemployeetManager {
     private void createAndStoreEmployee(String firstNm, String lastNm) {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          Employee theEmp = new Employee();
          theEmp.setFirstName(firstNm);
          theEmp.setLastName(lastNm);
          session.save(theEmployee);
          session.getTransaction().commit();
     }
     @SuppressWarnings("unchecked")
     private List<Employee> listEmployee() {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          List<Employee> result = session.createQuery("from Eemployee").list();
          session.getTransaction().commit();
          return result;
     }
     }



15         Hibernate Basic                                       May 2012
Example artifacts in Hibernate Architecture



       EmployeeManager.java     Application   HibernateUtil.java



                          Persistent Objects
                              Employee.java

        Configuration           Hibernate                    Mapping
     Hibernate.cfg.xml                                    Employee.hbm.xm
                                                                 l

                                 Database
                         jdbc:hsql:file:testdb




16    Hibernate Basic                          May 2012
Demo !!!




17   Hibernate Basic              May 2012
Questions and Answers


     Choose the correct option:
     ORM framework is the one which:


     a. Abstracts communication with object-oriented
        database.
     b. Abstracts communication with Relational
        database.
     c. Acts as database driver.
     d. Is a replacement of Java EE Application
        Container.




18        Hibernate Basic                              May 2012
Questions and Answers


     Choose the correct option:
     We can configure JNDI name for session factory to which
       Hibernate can automatically bind its _.
     a. SessionFactory
     b. JMX deployment
     c. hibernate.dialect




19      Hibernate Basic                 May 2012
Questions and Answers



     Which of the following is incorrect about <property> element?


     a. Maps to attributes of the entity class
     b. Can be computed dynamically
     c. Column attribute is optional
     d. Name attribute can only start with a capital letter




20         Hibernate Basic                                May 2012
Questions and Answers


     Choose the correct option:
     Hibernate dialects are used in configuration to:
     a. Connect to database instance.
     b. Replace database driver
     c. Make Hibernate generate database specific queries.




21        Hibernate Basic                               May 2012
22   Hibernate Basic   May 2012

More Related Content

What's hot (16)

PPT
jpa-hibernate-presentation
John Slick
 
PDF
Hibernate Interview Questions
Syed Shahul
 
PDF
Hibernate Presentation
guest11106b
 
ODP
Java EE and Glassfish
Carol McDonald
 
PDF
Tune my Code! Code-Versionen testen via Edition-Based Redef. - Jérôme Witt, d...
dbi services
 
PDF
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
DOCX
Hibernate3 q&a
Faruk Molla
 
PPT
09 transactions new
thirumuru2012
 
PPSX
Spring - Part 3 - AOP
Hitesh-Java
 
PPSX
Hibernate - Part 1
Hitesh-Java
 
PDF
Big data: current technology scope.
Roman Nikitchenko
 
PPT
09 transactions new1
thirumuru2012
 
PPTX
Technical Interview
prashant patel
 
DOCX
Java questions with answers
Kuntal Bhowmick
 
PDF
Hadoop 101
Nader Ganayem
 
PDF
Java Web Programming [3/9] : Servlet Advanced
IMC Institute
 
jpa-hibernate-presentation
John Slick
 
Hibernate Interview Questions
Syed Shahul
 
Hibernate Presentation
guest11106b
 
Java EE and Glassfish
Carol McDonald
 
Tune my Code! Code-Versionen testen via Edition-Based Redef. - Jérôme Witt, d...
dbi services
 
Introduction to JPA and Hibernate including examples
ecosio GmbH
 
Hibernate3 q&a
Faruk Molla
 
09 transactions new
thirumuru2012
 
Spring - Part 3 - AOP
Hitesh-Java
 
Hibernate - Part 1
Hitesh-Java
 
Big data: current technology scope.
Roman Nikitchenko
 
09 transactions new1
thirumuru2012
 
Technical Interview
prashant patel
 
Java questions with answers
Kuntal Bhowmick
 
Hadoop 101
Nader Ganayem
 
Java Web Programming [3/9] : Servlet Advanced
IMC Institute
 

Similar to Hibernate 18052012 (20)

PPT
Basic Hibernate Final
Rafael Coutinho
 
PPTX
Hibernate
Prashant Kalkar
 
PPTX
Hibernate
Sujit Kumar
 
PPT
Hibernate
Shaharyar khan
 
PPTX
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
PDF
Free Hibernate Tutorial | VirtualNuggets
Virtual Nuggets
 
PDF
Hibernate presentation
Luis Goldster
 
PDF
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
PDF
Hibernate reference1
chandra mouli
 
PPTX
Hibernate in Action
Akshay Ballarpure
 
PPT
Learn HIBERNATE at ASIT
ASIT
 
PPT
Patni Hibernate
patinijava
 
PPT
Hibernate
Murali Pachiyappan
 
PPTX
Hibernate
Mallikarjuna G D
 
PPS
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
PPT
Introduction to Hibernate
Krishnakanth Goud
 
PPT
Hibernate java and_oracle
Krishnakanth Goud
 
PDF
What is hibernate?
kanchanmahajan23
 
PPT
2010 05-21, object-relational mapping using hibernate v2
alvaro alcocer sotil
 
PPT
Hibernate introduction
Sagar Verma
 
Basic Hibernate Final
Rafael Coutinho
 
Hibernate
Prashant Kalkar
 
Hibernate
Sujit Kumar
 
Hibernate
Shaharyar khan
 
Module-3 for career and JFSD ppt for study.pptx
ViratKohli78
 
Free Hibernate Tutorial | VirtualNuggets
Virtual Nuggets
 
Hibernate presentation
Luis Goldster
 
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
Hibernate reference1
chandra mouli
 
Hibernate in Action
Akshay Ballarpure
 
Learn HIBERNATE at ASIT
ASIT
 
Patni Hibernate
patinijava
 
Hibernate
Mallikarjuna G D
 
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Introduction to Hibernate
Krishnakanth Goud
 
Hibernate java and_oracle
Krishnakanth Goud
 
What is hibernate?
kanchanmahajan23
 
2010 05-21, object-relational mapping using hibernate v2
alvaro alcocer sotil
 
Hibernate introduction
Sagar Verma
 
Ad

Hibernate 18052012

  • 1. Hibernate Introduction and Overview >By Manisha May 2012
  • 2. Agenda At the end of this course, you should be able to:  Describe the basics of Hibernate  Use Hibernate configuration  Explain basic O/R Mapping  Write simple application in Hibernate ● Q&A 2 Hibernate Basic May 2012
  • 3. Hibernate ● ORM technology  Consistent database communication and Increased maintainability: In Hibernate database communication policies are applied as configuration, in contrast as to code them in SQL based communication.  Developers with limited SQL knowledge can participate in development  Reduced testing time  Performance Optimization: Fine tuning database communication, adopting caching strategies can be done with configuration.  Portable code across multiple databases: Hibernate API and HQL can facilitate database neutral programming. 3 Hibernate Basic May 2012
  • 4. What is ORM?  Object Relational Mapping (ORM) frameworks are those which encapsulate SQL communication with relational databases, with object- oriented communication.  ORM is a programming way for converting data between incompatible type systems in relational databases and object-oriented programming languages.  Currently, several ORM frameworks such as Entity beans in Enterprise JavaBeans (EJB), TopLink from Oracle, iBatis, Hibernate, and so on are available. 4 Hibernate Basic May 2012
  • 5. Difference in conventional and ORM based implementation Conventional Implementation Service Data Access Database Object Object Implementation with ORM Data Service Hibernate Access Object Database Object 5 Hibernate Basic May 2012
  • 6. High level Hibernate Architecture Application Persistent Objects Hibernate Configuration Mapping Database 6 Hibernate Basic May 2012
  • 7. Basic content of Hibernate application In any standard hibernate application will have below data  Pojo class of table  Configuration files(hibernate.cfg.xml)  Mapping file(xxx.hbm.xml)  Application code to create Hibernate Runtime and execute 7 Hibernate Basic May 2012
  • 8. How to use Hibernate for data communication? Follow these steps to use Hibernate for data communication:  Prepare domain objects (POJO) for table.  Write mapping information.  Write Hibernate configuration.  Create Hibernate Runtime.  Use Hibernate Runtime to communicate with database. 8 Hibernate Basic May 2012
  • 9. Hibernate Example: Prepare pojo object(Class to table mapping) package com.ibm.tf.hibernate; public class Employee implements Serializable{ // attributes - relate to business entities private Integer id; private firstName; private lastName; // accessor and mutator methods public Integer getId() { //implementation code } …. public void printEmployeeName() { // some business method } } 9 Hibernate Basic May 2012
  • 10. Write mapping information(Table mapping to conf. Xml file) <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <!– Employee class mapping to EMPLOYEE table--> <class name=" com.ibm.tf.hibernate.Employee“ table=“EMPLOYEE"> <!– identifier column mapping--> <id name="id“ column=“ID“ type="long"> <generator class="native"/> </id> <!– property mapping---> <property name=“firstName“ column=“FIRST_NAME“ type="string"/> <property name=“lastName“ column=“LAST_NAME“ type="string"/> </class> </hibernate-mapping> 10 Hibernate Basic May 2012
  • 11. Write Hibernate configuration information Hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.url">jdbc:hsqldb:file:testdb</property> <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/hibernate/test/Employee.hbm.xml"/> </session-factory> </hibernate-configuration> 11 Hibernate Basic May 2012
  • 12. Write Hibernate configuration information The following are illustrative (not exhaustive) configuration parameters sorted by their categories. With category description we can guess on what kind of values could be assigned to individual parameters.  Hibernate JDBC Properties (useful for standalone) – hibernate.connection.driver_class – hibernate.connection.url – hibernate.connection.username – hibernate.connection.password  Hibernate Datasource Properties (useful inside container) – hibernate.connection.datasource – hibernate.jndi.url  List of managed entities  Hibernate Configuration Properties: – hibernate.dialect n hibernate.order_updates 12 Hibernate Basic May 2012
  • 13. Create Hibernate runtime Before creating hibernate runtime, lets understand It  Hibernate Session, Transaction, and all Objects working behind them are collectively called as Hibernate Runtime.  Information like how to connect to database, how to map entity data to table row, how to handle transactions and many more are provided to Hibernate Runtime by means of configuration.  As Hibernate is designed to operate in many different environments. Hence, there are: – Many configurable parameters of various category. – Many ways to configure Hibernate Runtime. 13 Hibernate Basic May 2012
  • 14. Create hibernate runtime public class HibernateUtil { private static final SessionFactory sessionFactory; static { try {// Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 14 Hibernate Basic May 2012
  • 15. Use Hibernate Runtime public class EemployeetManager { private void createAndStoreEmployee(String firstNm, String lastNm) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Employee theEmp = new Employee(); theEmp.setFirstName(firstNm); theEmp.setLastName(lastNm); session.save(theEmployee); session.getTransaction().commit(); } @SuppressWarnings("unchecked") private List<Employee> listEmployee() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<Employee> result = session.createQuery("from Eemployee").list(); session.getTransaction().commit(); return result; } } 15 Hibernate Basic May 2012
  • 16. Example artifacts in Hibernate Architecture EmployeeManager.java Application HibernateUtil.java Persistent Objects Employee.java Configuration Hibernate Mapping Hibernate.cfg.xml Employee.hbm.xm l Database jdbc:hsql:file:testdb 16 Hibernate Basic May 2012
  • 17. Demo !!! 17 Hibernate Basic May 2012
  • 18. Questions and Answers Choose the correct option: ORM framework is the one which: a. Abstracts communication with object-oriented database. b. Abstracts communication with Relational database. c. Acts as database driver. d. Is a replacement of Java EE Application Container. 18 Hibernate Basic May 2012
  • 19. Questions and Answers Choose the correct option: We can configure JNDI name for session factory to which Hibernate can automatically bind its _. a. SessionFactory b. JMX deployment c. hibernate.dialect 19 Hibernate Basic May 2012
  • 20. Questions and Answers Which of the following is incorrect about <property> element? a. Maps to attributes of the entity class b. Can be computed dynamically c. Column attribute is optional d. Name attribute can only start with a capital letter 20 Hibernate Basic May 2012
  • 21. Questions and Answers Choose the correct option: Hibernate dialects are used in configuration to: a. Connect to database instance. b. Replace database driver c. Make Hibernate generate database specific queries. 21 Hibernate Basic May 2012
  • 22. 22 Hibernate Basic May 2012