SlideShare una empresa de Scribd logo
1 de 100
Hibernate 3.0

       Rajeev Gupta
        M. Tech CS
Workshop topics

   Hibernate-What it is ?         Hibernate Unidirectional
                                    Mapping
   ORM and Issues
                                     One to many
   Hibernate Hello World CRUD       one to one
   Hello world with Servlet         many to one
   Hibernate Object life cycle      many to many
   Hibernate Architecture
   Object as Component mapping    Hibernate Bidirectional
                                    mapping
   Hibernate Inheritance
                                   HQL
                                   Native SQL queries
                                   Named Quarries
Hibernate-What it is ?
What is Hibernate 3

       Hibernate is an Object relation mapping framework since 2001 form
        JBoss, a division of Red Hat

       It provides tools to get Java Objects in an out of an database

       It helps to get their relationship to other object in the database

       Classes in Hibernate are plain old java objects and so like any
        normal java classes they can participate in relationships like
        association, inheritance, polymorphism, composition, and collections.

       Provides query and retrieval services

    4
Hibernate modules of discussion
               Our area

Hibernate Core        Hibernate for Java, native APIs and XML mapping
                      metadata
Hibernate             Standard Java Persistence API for Java SE and
EntityManager         Java EE

Hibernate             Map classes with JDK 5.0 annotations
Annotations
Hibernate Shards      Horizontal data partitioning framework
Hibernate Validator   Data integrity annotations and validation API
Hibernate Search      Hibernate integration with Lucene for indexing and
                      querying data
Hibernate Tools       Development tools for Eclipse and Ant
NHibernate            The NHibernate service for the .NET framework
JBoss Seam            Framework for JSF, Ajax, and EJB 3.0/Java EE 5.0
  5
                      applications
Why Hibernate?
       No JDBC code writing
       Can be used in both managed(application server) and
        unmanaged environments( standalone application)
       Common way to persistence development for both .NET
        and JEE.
       SQL can be combined with an object-oriented approach.




    6
Hibernate in java application
       Can be used with
           Unmanaged environment
             Stand-alone application
           Managed environment
             Web application
             Enterprise application (can be used in place of entity beans)



       The same power of SQL can be used with hibernate also.




    7
ORM & Issues
Object Relational Mapping
ORM
 Object           Relational
   Mapping object to relational world is not easy, as there are points of
    mismatches

   There are numbers of impedance mismatches between OO world and DB
    worlds, and ORM tool must address and provide solutions for that

   Identity
   Granularity
   Associations
   Navigation
   Inheritance
   Data type mismatch
Identity
   A row is uniquely identified form all other rows by its
    primary key
   An object's identity does not always translate well to the
    concept of primary key of DB world
   In Java, What is the meaning of identity of an object?
       Its data or its place in memory?
Granularity
   Objects and tables are at different levels of granularity

   Table structure are often de-normalized in order to
    support better performance, so table rows can map to
    multiple objects, how to map them?
Associations
   Associations in Java are either unidirectional or
    bidirectional and can be represented by one object having
    pointer of other object.
   Relation between database tables is based on join. table
    relationship is always bidirectional, while for object it may
    be unidirectional/bidirectional
Association Example
   Java Objects




   Data tables
Navigation and associations traversal
   Navigating Java Objects to get property information often
    require traversal of the object graph.
       This can be quite expensive if the objects are distributed or
        need to be created before traversed.
       Consider getting the address to ship and order




   While navigation in database is handled with the help of
    single join.
Inheritance
   Inheritance and Polymorphism are important concepts in
    OO world
   Database don't have equivalent concepts
   Now question is how to map it to database world?

Inheritance examples
Data Types
   There is mismatch between database data type and object
    oriented data types.
   Question is how ORM tools can help us to map is
    correctly?
Hibernate Handles all these issues
   Hibernate provides many options to handle all the
    previous issues...
Hibernate Hello World CRUD
High-level Hibernate Architecture
Hibernate Hello World
   Steps:
     1.   Write a POJO class representing the table
     2.   Write a hbm file
     3.   Write cfg file
     4.   Write a class to test CRUD code




                                                      20
Write a POJO class representing the table
Hibernate.cfg.xml
Person.hbm.xml
Adding record




   Result:
Deleting record
Updating record
Display all records
Hello world with servlet
Simple Hibernate application
   Steps:
     1.   Write a POJO class representing the table
     2.   Write a hbm file
     3.   Write cfg file
     4.   Write a servlet code to connect through hibernate and insert data into
          the table.




                                                           29
POJO




       30
Write a hbm file




 Other ways to write property:
       <property name="firstName"><column name="FIRSTNAME"/>
       </property>
 Or simply
            <property name="firstName“ column=“FIRSTNAME”/>

 Type can be explicitly specifies as:
       <property name="name" type="java.lang.String" column=" firstName
       " length="50" />
Write cfg file




 cfg file is a Hibernate configuration file that details about the database that is
  going to be used by hibernate.
 File name is hibernate.cfg.xml.
 File must be in the same place where classes are located.
 Create an XML file with the name hibernate.cfg.xml in the src folder.
7. Write a servlet




                     33
Hibernate Object life cycle
Object lifecycle

   Persistent objects (like Person object) are synchronized with
    the database.
       That is, the objects state and the affected database rows are kept the
        same whenever either changes!

   Domain object are in one of these states;
           Transient
           Persistence
           Detached
   An objects state determine if it is kept synchronized with the
    database
   The presence of a session and certain method calls move an
    object between states.
Object States
Hibernate Architecture
Complete Hibernate Architecture




                            38
Core Interfaces
   Used to perform basic CRUD and querying operations
       Session
       SessionFactory
       Configuration
       Transaction interface
       Query
       Criteria




                                          39
Session
   Primary interface used by Hibernate applications to create, read and delete
    operations for instances of mapped entity classes

   This instance wraps the JDBC connection and is also used to create
    transactions.

   The persistent objects (POJO) are associated with exactly one Session.
    Once the Session is closed, they are detached.

   Instance is created using SessionFactory
     Session openSession() throws HibernateException

   Session instance is not thread-safe.
   Instead each thread/transaction should obtain its own instance from a
    SessionFactory.


                                                          40
SessionFactory
   The application obtains Session instances from a SessionFactory
    interface
   The SessionFactory is created from an configuration object.

           SessionFactory sf=cfg.buildSessionFactory();

   The SessionFactory is an expensive object to create

   It too is created at application start-up time
       It should be created once and kept for latter use.
       The SessionFactory object is used by all the threads of an applications
       it is thread safe object
       one SessionFactory object is created per database (where connecting to multiple
        sessions)

   The SessionFactory is used to create Session Objects
Session
   The Session object is created from the SessionFactory object

           Session session=sf.openSession();

       A Session object is lightweight and inexpensive to create.
       Session object provides the main interface to accomplish work with the
        database
       Does the work of getting a physical connection to the database
        (hopefully from a connection pool)
       Session objects are not thread safe
       Session objects shold not be kept open for a long time
       Application create and destroy these as needed. Typically they are
        created to complete a single unit of work.

   When modifications are made to the database, session objects are
    used to create a transaction object
Transaction
   Transaction objects are obtained from the session objects,
    when an modification to the database is needed.

           Transaction transaction =session.beginTransaction();

   The Transaction object provides abstraction for the underlying
    implementation

       Hibernate with use whatever transaction implementation is available
        (JDBC, JTA etc.)

       It is optional; allowing developer to use their own transactional
        infrastructure.

       Transaction objects should be kept open for a short time.
How hibernate actually works with JDBC?

   By default Hibernate creates a proxy for each of the
    entity class in mapping file. This class contain the code to
    invoke JDBC.
   Proxies are created dynamically by sub-classing the entity
    object at runtime.
   The subclass has all the methods of the parent, so when a
    method on the entity object is called, the proxy loads up
    the data from the database and calls the method.




                                               44
Persistent object states
   The POJO or persistent objects can be in one of the
    three states is defined in relation to a persistence context
    (that means it is loaded into the Hibernate Session object)
       Transient
       Persistent
       Detached




                                                 45
Transient state

   The instance is not associated with any persistence context.
    It has no persistent identity or primary key value.
   Transient instances may be made persistent by calling
    save(), persist() or saveOrUpdate() of
    Session
       Serializable save(Object object)                      throws
        HibernateException
           Persist the given transient instance, first assigning a generated identifier or
            using the current value of the identifier property if the assigned generator
            is used. This operation cascades to associated instances if the association is
            mapped with cascade="save-update".



                                                                   46
   void saveOrUpdate(Object object) throws
    HibernateException
     Either save(Object) or update(Object) the given instance,
      depending upon resolution of the unsaved-value checks.
     This operation cascades to associated instances if the association is
      mapped with cascade="save-update”

   void persist(Object object)                 throws
    HibernateException
       Make a transient instance persistent. This operation cascades to
        associated instances if the association is mapped with cascade="persist".




                                                           47
Why do we have 2 methods – save() and
  persist()?
 With save() the insert statement is executed immediately regardless of
   transaction state. It returns the inserted key so you can do something like this:
         long newKey = session.save(myObj); So use save() if you need an identifier
         assigned to the persistent instance immediately.
 With persist(), the insert statement is executed in a transaction, not necessarily
   immediately.This is preferable in most cases.



Use persist() if you don't need the insert to happen out-of-sequence
   with the transaction and you don't need the inserted key
   returned.
                                                             48
get() and load()
    The instance is currently associated with a persistence context. It has a persistent
     identity (primary key value) and can have a corresponding row in the database.
    Any instance returned by a get() or load() method is persistent. Persistent
     instances may be made transient by calling delete().

       Object get(Class clazz, Serializable id) throws
        HibernateException
           Return the persistent instance of the given entity class with the given identifier, or null if
            there is no such persistent instance.
           If the instance is already associated with the session, return that instance. This method never
            returns an uninitialized instance.


       Object load(Class clazz,                          Serializable id)                   throws
        HibernateException
       Return the persistent instance of the given entity class with the given identifier,
        assuming that the instance exists. This method might return a proxied instance
        that is initialized on-demand, when a non-identifier method is accessed.
                                                                  49
get() and load()

Difference between the load() and get() methods is that while
load() assumes that instance exists and may return a proxy and
we cannot guarantee that instance actually exists in the
database (it may have got deleted).
Hence get() must be used instead of load() to determine if an instance exists.
Also note that the load() will throw an exception if instance does not exist.




                                                            50
Detached
   The instance was once associated with a persistence
    context/session but not is not attached to it may because the
    session was closed.
   It has a persistent identity and can have a corresponding row in
    the database.
   While for persistent instance Hibernate guarantees the
    relationship between persistent (database ) identity and Java
    identity for detached instance there is no such guarantees .
   Detached instances may be made persistent by calling
    update(), saveOrUpdate()
   The state of a transient or detached instance may also be made
    persistent as a new persistent instance by calling merge().



                                                   51
update()and merge()

   void update(Object object)                            throws
    HibernateException
       Update the persistent instance with the identifier of the given detached instance.
       If there is a persistent instance with the same identifier, an exception is thrown.
       This operation cascades to associated instances if the association is mapped
        with cascade="save-update".
   Object merge(Object object)                             throws
    HibernateException
       Copy the state of the given object onto the persistent object with the same
        identifier.
       This operation cascades to associated instances if the association is mapped
        with cascade="merge".




                                                                   52
Example: update(),merge()differences
To understand difference bw update() and merge() lets try to
update values inserted earlier in servlet…




                                              53
When
session2.update(c); is
used instead of
session2.merge(c);

This is if there is a persistent
instance with the same
identifier in the session,
update() will throw an
exception!


 When
 session2.merge(c);
 is used the data gets updated
 because the instance that the
 session is holding gets its
 values from the detached
 instance when merge is called
 on detached instance.
         54
delete() and refresh()
   void delete(Object object) throws
    HibernateException
       Remove a persistent instance from the datastore. The argument may be an
        instance associated with the receiving Session or a transient instance with
        an identifier associated with existing persistent state. This operation
        cascades to associated instances if the association is mapped with
        cascade="delete".
    void refresh(Object object) throws
     HibernateException
    This method is useful to sync the state of the given instance with underlying
    database in cases where there are chances that database might have been
    altered outside the application as a result of a trigger. This is also useful in
    cases where direct SQL is used by the application to update the data.



                                                               55
flush(), close(), clear
   void flush() throws HibernateException
       This is the method that actually synchronizing the underlying database with
        persistent object held in session memory.
       This should be called at the end of a unit of work, before committing the
        transaction and closing the session (depending on flush-mode,
        Transaction.commit() calls this method).
   Connection close()                     throws HibernateException
       End the session by releasing the JDBC connection and cleaning up. It is not strictly
        necessary to close the session but at least it must be disconnected by calling
        disconnect()
   void clear()
       Completely clear the session. Evict all loaded instances and cancel all pending
        saves, updates and deletions. Do not close open iterators or instances of
        ScrollableResults.
                                                                    56
Transaction

   Transaction can be set using the methods of Session.
    Transaction beginTransaction() throws HibernateException
    If Transaction object is associated with the session return that else create a new
    transaction.
    Transaction getTransaction()
    Get the Transaction instance associated with this session
    In both the cases the class of the returned Transaction object is determined
    by the property hibernate.transaction_factory.
   The most common approach is the session-per-request pattern
    where a single Hibernate Session has same scope as a single
    database transaction.
   The Hibernate session can be used for multiple DB operations (save,
    query, update) within the same request.
                                                                  57
Transaction methods
   void begin() throws HibernateException
   void commit() throws HibernateException
   void rollback() throws HibernateException
   To check if transactions was committed or rolled-back properly
   boolean wasRolledBack() throws HibernateException
   boolean wasCommitted() throws HibernateException
   Code snippet wrapping statements in a transactions:
     Session sess = factory.openSession();
     Transaction tx;
     try { tx = sess.beginTransaction();
     //do some work ...
     tx.commit();
     }
     catch (Exception e) {
         if (tx!=null) tx.rollback(); throw e; }
     finally { sess.close(); }                            58
Mapping file

   We have seen in the last session that the mapping file tells
    Hibernate what table in the database it has to access, and
    what columns in that table it should use.
   All persistent entity classes go in between the
    <hibernate-mapping> tags, include a class element.
   We have also looked at how to map the bean columns to
    the database tables. By default, no properties of the class
    are considered persistent unless it is specified in hbm.
   Some more important things to know about the mapping
    files are
       Key generation
       Relationship management
       Lazy loading
                                                 59
Hibernate persistent class
   Class should have a constructor with no arguments.
   Implementing Serializable is not compulsory.
    However if the object in to be stored in HttpSession
    then it will be necessary to make the persistent class
    Serializable.
   The instance variables of the class represent attributes of
    business entity. The accessor and mutator methods needs
    to be provided for them as laid out by JavaBean
    specification guidelines. However the methods may not be
    declared public.
   Can contain some business methods also.

                                               60
Accessor code
   Hibernate uses accessor methods to populate the state
    of an object when loading the object from the database.
   But what if we have a validation code in the setter
    method and we don’t want hibernate to run this
    validation while loading the data?
   In that case, we need to tell Hibernate to directly access
    the instance variables
       <property name=“fname“ type=“java.lang.String"
        column=“fname” access="field”>
    forcing Hibernate to bypass the setter method and access
    the instance variable directly.

                                               61
Key generation
 <id name="id" type="long" column="ID" >
   <generator class="native"/>
  </id>
There is an alternative <composite-id> declaration that
allows access to legacy data with composite keys. Its use is
strongly discouraged for anything else.
 The optional <generator> child element names a Java
   class used to generate unique identifiers for instances of
   the persistent class.
 Hibernate provides a range of built-in key
   implementations.

                                              62
List of Key generator
Generator   Description

            It generates identifiers of type long, short or int
            that are unique only when no other process is
increment
            inserting data into the same table. It should not
            the used in the clustered environment.
            It supports identity columns in DB2, MySQL, MS
identity    SQL Server, Sybase and HypersonicSQL. The
            returned identifier is of type long, short or int.
            The sequence generator uses a sequence in
            DB2, PostgreSQL, Oracle, SAP DB, McKoi or a
sequence
            generator in Interbase. The returned identifier is
            of type long, short or int

            lets the application to assign an identifier to the
assigned    object before save() is called. This is the default
            strategy if no <generator> element is specified.
                                                     63
Generator   Description
            The hilo generator uses a hi/lo algorithm to efficiently
            generate identifiers of type long, short or int, given a table
            and column (by default hibernate_unique_key and next_hi
hilo        respectively) as a source of hi values. The hi/lo algorithm
            generates identifiers that are unique only for a particular
            database. Do not use this generator with connections
            enlisted with JTA or with a user-supplied connection.
            The seqhilo generator uses a hi/lo algorithm to efficiently
seqhilo     generate identifiers of type long, short or int, given a named
            database sequence.
            The uuid generator uses a 128-bit UUID algorithm to
            generate identifiers of type string, unique within a network
uuid
            (the IP address is used). The UUID is encoded as a string of
            hexadecimal digits of length 32.
            It uses a database-generated GUID string on MS SQL
guid
            Server and MySQL.

            It picks identity, sequence or hilo depending upon the
native
            capabilities of the underlying database. 64
Generator   Description
            retrieves a primary key assigned by a database trigger by
select      selecting the row by some unique key and retrieving the
            primary key value

            uses the identifier of another associated object. Usually used
foreign
            in conjunction with a <one-to-one> primary key association.




                                                    65
Object as Component mapping
Object as Component
   Associated Objects: <Components>
   Set of Associated objects: <Composite-element>
   Components can not exist their own, they share id of the
    parent entity
   Lets say Person have many attributes and we want to
    divide it into two POJO such as Person and Address

   Steps:
    1.   Put reference of Address in Person class
    2.   Map it into mapping file..as shown on next slide.
Object as Component
Set as Composition
   Just remove <component.../> with
          <set name="addresses">
                     <key column="aid"></key>
                     <composite-element class="Address">
                               <property name="city"></property>
                               <property name="state"></property>
                     </composite-element>
          </set>


Why?
<key column="aid“/>
As one person has N address, we need to have seperate table with name addresses and fk =aid
   that is same as id of person
Hibernate Inheritance mapping
Inheritance
    Super class & sub class relationship

    Types of inheritance relations in hibernate

1.    Table per concrete class <union-subclass>
2.    Table per class hierarchy <subclass>
3.    Table per class <joined-subclass>
Inheritance
Table per concrete class <union-subclass>

       here Account class is an abstract class. So no need to map it to database, the
        values mapped by sub classes ie SavingAccount and CurrentAccount
       Two separate table is going to be created for SavingAccount and
        CurrentAccount

   Table per class hierarchy <subclass>

       Here only one table is going to be created, all fields mapped to single table.
       Not very memory efficient, May be faster

   Table per class <joined-subclass>
       Separate table mapped to all classes in
        the hierarchy
Table per concrete class <union-subclass>
Table per class hierarchy <subclass>
   Single table is created per hierarchy ie some columns of
    table may contain nulls.
Table per class <joined-subclass>
   Table for each class is going to be created.
Composite primary key
 ore then one field combined as primray key. let consider
public class Person {
           private int id;
           private String name;
           private String city;
           private String country;

   lets say we want to consider id +name as composite pk
   better to create an seperate table for ID
   Create an seperate class ie PersonID
Composite primary key
Hibernate Unidirectional Mapping
 One to many
 one to one
 many to one
 many to many
One to many
   For example one person has N addresses.
One to many
                 cascase=“all”
                     If underlying person
                      record is changed
                      corresponding Address
                      get effected.
                     Id act as FK in Address
                      table.
one to one
   For example one person can have only one addresses.
Many to One
   Many person can have same address.
Many to One
many to many
   Many persons can have may address
   We need third table to map these two tables.
many to many
Hibernate Bidirectional mapping
 One to many
 one to one
 many to one
 many to many
One to many
bi-directional
   In bi-directional mapping we can navigate from both the
    directions.
   i.e. One can navigate from Person to Address and vice
    versa.
One to many
bi-directional mapping
Hibernate Query examples (HQL)
HQL
   Hibernate Query Language (HQL) is used similarly to SQL
   The main difference between is HQL uses class name instead of table
    name, and property names instead of column name.
   HQL is extremely simple to learn and use, and the code is always self-
    explanatory


   We have already used HQL if you remember:
Some more examples
HQL Delete Query Example
HQL Insert Query Example
   In HQL, only the INSERT INTO … SELECT … is
    supported; there is no INSERT INTO … VALUES.
   HQL only support insert from another table. For example
Native SQL queries
Native SQL queries
   In Hibernate, HQL or criteria queries should be able to
    let you to execute almost any SQL query you want.

   However, many developers are complaint about the
    Hibernates generated SQL statement is slow and more
    prefer to generated their own SQL (native SQL)
    statement
Native SQL queries example
   Hibernate provide a createSQLQuery method to let
    you call your native SQL statement directly.
       In this example, you tell Hibernate to return you a Stock.class,
        all the select data (*) will match to your Stock.class properties
        automatically.




       In this example, Hibernate will return you an Object array
Named queries
Named queries
   Sometimes embedding HQL into Java code may make it harder to
    maintain.
   Named query helps to externalize any HQL queries that can be
    statically defined.
   The queries are specified in mapping document with the a name.
   They are retrieved using method defined in the Session

Query getNamedQuery(String queryName)                     throws
HibernateException

   Parameterized queries can also be written in the mapping document
    and this can be retrieved using the same approach that we discussed
    in the previous slide.

                                                     98
Example: Named queries
customer.hbm.xml
<hibernate-mapping>
   …
   <query name="getCustID">select c.id from Customer as
   c where c.email=?</query>
</hibernate-mapping>

In the main
//get Session etc
Query q = session.getNamedQuery("getCustID");
q.setString(0,"mm@yahoo.com");
List id = q.list();
out.println("ID is : " + id.get(0));
…….
……..


                                         99
Named Parameters
 One of the shortcomings of parameterized query setting
  properties using index is that if the index value changes then the
  query will not be correct.
 The Query’s method
      Query setXXX(String nm,XXX x)
      allows using named parameters instead of index values.
In customer.hbm.xml
<query name="getCustID">select c.id from
Customer as c where c.email=:email</query>
In the main
    Query q = session.getNamedQuery("getCustID");
    q.setString("email","mm@yahoo.com");
    List id = q.list();
    out.println("ID is : " + id.get(0));    100

Más contenido relacionado

La actualidad más candente

Hibernate Basic Concepts - Presentation
Hibernate Basic Concepts - PresentationHibernate Basic Concepts - Presentation
Hibernate Basic Concepts - PresentationKhoa Nguyen
 
Hibernate
HibernateHibernate
HibernateAjay K
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginnersRahul Jain
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Examplekamal kotecha
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkRaveendra R
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewBrett Meyer
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesEr. Gaurav Kumar
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questionsArun Vasanth
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernateashishkulkarni
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernatehr1383
 

La actualidad más candente (19)

Hibernate Basic Concepts - Presentation
Hibernate Basic Concepts - PresentationHibernate Basic Concepts - Presentation
Hibernate Basic Concepts - Presentation
 
Hibernate
HibernateHibernate
Hibernate
 
Introduction to Hibernate
Introduction to HibernateIntroduction to Hibernate
Introduction to Hibernate
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginners
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questions
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernate
 
Hibernate
HibernateHibernate
Hibernate
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
 

Destacado

Cursors in oracle
Cursors in oracleCursors in oracle
Cursors in oracleTamizhmuhil
 
SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
SQL Server MERGE statement and recursive queries for the 70-461 CertificatoinSQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
SQL Server MERGE statement and recursive queries for the 70-461 CertificatoinSteve Stedman
 
Hands On with Maven
Hands On with MavenHands On with Maven
Hands On with MavenSid Anand
 
Build Automation using Maven
Build Automation using Maven Build Automation using Maven
Build Automation using Maven Ankit Gubrani
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippersSARITHA REDDY
 
Distributed shared memory shyam soni
Distributed shared memory shyam soniDistributed shared memory shyam soni
Distributed shared memory shyam soniShyam Soni
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To HibernateAmit Himani
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVCDzmitry Naskou
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
Clippers and clampers
Clippers and clampersClippers and clampers
Clippers and clamperstaranjeet10
 

Destacado (16)

Cursors in oracle
Cursors in oracleCursors in oracle
Cursors in oracle
 
Cursors
CursorsCursors
Cursors
 
SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
SQL Server MERGE statement and recursive queries for the 70-461 CertificatoinSQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
SQL Server MERGE statement and recursive queries for the 70-461 Certificatoin
 
Sql DML
Sql DMLSql DML
Sql DML
 
Hands On with Maven
Hands On with MavenHands On with Maven
Hands On with Maven
 
Build Automation using Maven
Build Automation using Maven Build Automation using Maven
Build Automation using Maven
 
Clipper circuits
Clipper circuitsClipper circuits
Clipper circuits
 
Maven and ANT
Maven and ANTMaven and ANT
Maven and ANT
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippers
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
 
Distributed shared memory shyam soni
Distributed shared memory shyam soniDistributed shared memory shyam soni
Distributed shared memory shyam soni
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Clippers and clampers
Clippers and clampersClippers and clampers
Clippers and clampers
 

Similar a Hibernate 3

Learn HIBERNATE at ASIT
Learn HIBERNATE at ASITLearn HIBERNATE at ASIT
Learn HIBERNATE at ASITASIT
 
Free Hibernate Tutorial | VirtualNuggets
Free Hibernate Tutorial  | VirtualNuggetsFree Hibernate Tutorial  | VirtualNuggets
Free Hibernate Tutorial | VirtualNuggetsVirtual Nuggets
 
Java hibernate orm implementation tool
Java hibernate   orm implementation toolJava hibernate   orm implementation tool
Java hibernate orm implementation tooljavaease
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1b_kathir
 
Hibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaHibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaEdureka!
 
Hibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersHibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersAnuragMourya8
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006achraf_ing
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questionsDhiraj Champawat
 

Similar a Hibernate 3 (20)

Hibernate
HibernateHibernate
Hibernate
 
Learn HIBERNATE at ASIT
Learn HIBERNATE at ASITLearn HIBERNATE at ASIT
Learn HIBERNATE at ASIT
 
Free Hibernate Tutorial | VirtualNuggets
Free Hibernate Tutorial  | VirtualNuggetsFree Hibernate Tutorial  | VirtualNuggets
Free Hibernate Tutorial | VirtualNuggets
 
Java hibernate orm implementation tool
Java hibernate   orm implementation toolJava hibernate   orm implementation tool
Java hibernate orm implementation tool
 
Hibernate Session 1
Hibernate Session 1Hibernate Session 1
Hibernate Session 1
 
What is hibernate?
What is hibernate?What is hibernate?
What is hibernate?
 
Hibernate3 q&a
Hibernate3 q&aHibernate3 q&a
Hibernate3 q&a
 
Hibernate Interview Questions | Edureka
Hibernate Interview Questions | EdurekaHibernate Interview Questions | Edureka
Hibernate Interview Questions | Edureka
 
What is hibernate?
What is hibernate?What is hibernate?
What is hibernate?
 
Hibernate training-topics
Hibernate training-topicsHibernate training-topics
Hibernate training-topics
 
Hibernate
HibernateHibernate
Hibernate
 
What is hibernate?
What is hibernate?What is hibernate?
What is hibernate?
 
Hibernate
HibernateHibernate
Hibernate
 
Hibernate Interview Questions and Answers
Hibernate Interview Questions and AnswersHibernate Interview Questions and Answers
Hibernate Interview Questions and Answers
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
 
TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6
 
P20CSP105-AdvJavaProg.pptx
P20CSP105-AdvJavaProg.pptxP20CSP105-AdvJavaProg.pptx
P20CSP105-AdvJavaProg.pptx
 
Hibernate.pdf
Hibernate.pdfHibernate.pdf
Hibernate.pdf
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
Hibernate
HibernateHibernate
Hibernate
 

Más de Rajiv Gupta

Spring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepSpring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepRajiv Gupta
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with javaRajiv Gupta
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentalsRajiv Gupta
 
Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2Rajiv Gupta
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastRajiv Gupta
 
Lab work servlets and jsp
Lab work servlets and jspLab work servlets and jsp
Lab work servlets and jspRajiv Gupta
 
Spring aop with aspect j
Spring aop with aspect jSpring aop with aspect j
Spring aop with aspect jRajiv Gupta
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injectionRajiv Gupta
 
Java spring framework
Java spring frameworkJava spring framework
Java spring frameworkRajiv Gupta
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jRajiv Gupta
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notesRajiv Gupta
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuffRajiv Gupta
 

Más de Rajiv Gupta (18)

Spring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepSpring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by step
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with java
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentals
 
Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencast
 
Struts2
Struts2Struts2
Struts2
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Java 7
Java 7Java 7
Java 7
 
Struts2 notes
Struts2 notesStruts2 notes
Struts2 notes
 
Lab work servlets and jsp
Lab work servlets and jspLab work servlets and jsp
Lab work servlets and jsp
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
Spring aop with aspect j
Spring aop with aspect jSpring aop with aspect j
Spring aop with aspect j
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injection
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
 
Jsp Notes
Jsp NotesJsp Notes
Jsp Notes
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuff
 

Último

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Hibernate 3

  • 1. Hibernate 3.0 Rajeev Gupta M. Tech CS
  • 2. Workshop topics  Hibernate-What it is ?  Hibernate Unidirectional Mapping  ORM and Issues  One to many  Hibernate Hello World CRUD  one to one  Hello world with Servlet  many to one  Hibernate Object life cycle  many to many  Hibernate Architecture  Object as Component mapping  Hibernate Bidirectional mapping  Hibernate Inheritance  HQL  Native SQL queries  Named Quarries
  • 4. What is Hibernate 3  Hibernate is an Object relation mapping framework since 2001 form JBoss, a division of Red Hat  It provides tools to get Java Objects in an out of an database  It helps to get their relationship to other object in the database  Classes in Hibernate are plain old java objects and so like any normal java classes they can participate in relationships like association, inheritance, polymorphism, composition, and collections.  Provides query and retrieval services 4
  • 5. Hibernate modules of discussion Our area Hibernate Core Hibernate for Java, native APIs and XML mapping metadata Hibernate Standard Java Persistence API for Java SE and EntityManager Java EE Hibernate Map classes with JDK 5.0 annotations Annotations Hibernate Shards Horizontal data partitioning framework Hibernate Validator Data integrity annotations and validation API Hibernate Search Hibernate integration with Lucene for indexing and querying data Hibernate Tools Development tools for Eclipse and Ant NHibernate The NHibernate service for the .NET framework JBoss Seam Framework for JSF, Ajax, and EJB 3.0/Java EE 5.0 5 applications
  • 6. Why Hibernate?  No JDBC code writing  Can be used in both managed(application server) and unmanaged environments( standalone application)  Common way to persistence development for both .NET and JEE.  SQL can be combined with an object-oriented approach. 6
  • 7. Hibernate in java application  Can be used with  Unmanaged environment  Stand-alone application  Managed environment  Web application  Enterprise application (can be used in place of entity beans)  The same power of SQL can be used with hibernate also. 7
  • 8. ORM & Issues Object Relational Mapping
  • 9. ORM  Object Relational  Mapping object to relational world is not easy, as there are points of mismatches  There are numbers of impedance mismatches between OO world and DB worlds, and ORM tool must address and provide solutions for that  Identity  Granularity  Associations  Navigation  Inheritance  Data type mismatch
  • 10. Identity  A row is uniquely identified form all other rows by its primary key  An object's identity does not always translate well to the concept of primary key of DB world  In Java, What is the meaning of identity of an object?  Its data or its place in memory?
  • 11. Granularity  Objects and tables are at different levels of granularity  Table structure are often de-normalized in order to support better performance, so table rows can map to multiple objects, how to map them?
  • 12. Associations  Associations in Java are either unidirectional or bidirectional and can be represented by one object having pointer of other object.  Relation between database tables is based on join. table relationship is always bidirectional, while for object it may be unidirectional/bidirectional
  • 13. Association Example  Java Objects  Data tables
  • 14. Navigation and associations traversal  Navigating Java Objects to get property information often require traversal of the object graph.  This can be quite expensive if the objects are distributed or need to be created before traversed.  Consider getting the address to ship and order  While navigation in database is handled with the help of single join.
  • 15. Inheritance  Inheritance and Polymorphism are important concepts in OO world  Database don't have equivalent concepts  Now question is how to map it to database world? Inheritance examples
  • 16. Data Types  There is mismatch between database data type and object oriented data types.  Question is how ORM tools can help us to map is correctly?
  • 17. Hibernate Handles all these issues  Hibernate provides many options to handle all the previous issues...
  • 20. Hibernate Hello World  Steps: 1. Write a POJO class representing the table 2. Write a hbm file 3. Write cfg file 4. Write a class to test CRUD code 20
  • 21. Write a POJO class representing the table
  • 24. Adding record  Result:
  • 28. Hello world with servlet
  • 29. Simple Hibernate application  Steps: 1. Write a POJO class representing the table 2. Write a hbm file 3. Write cfg file 4. Write a servlet code to connect through hibernate and insert data into the table. 29
  • 30. POJO 30
  • 31. Write a hbm file Other ways to write property: <property name="firstName"><column name="FIRSTNAME"/> </property> Or simply <property name="firstName“ column=“FIRSTNAME”/> Type can be explicitly specifies as: <property name="name" type="java.lang.String" column=" firstName " length="50" />
  • 32. Write cfg file  cfg file is a Hibernate configuration file that details about the database that is going to be used by hibernate.  File name is hibernate.cfg.xml.  File must be in the same place where classes are located.  Create an XML file with the name hibernate.cfg.xml in the src folder.
  • 33. 7. Write a servlet 33
  • 35. Object lifecycle  Persistent objects (like Person object) are synchronized with the database.  That is, the objects state and the affected database rows are kept the same whenever either changes!  Domain object are in one of these states;  Transient  Persistence  Detached  An objects state determine if it is kept synchronized with the database  The presence of a session and certain method calls move an object between states.
  • 39. Core Interfaces  Used to perform basic CRUD and querying operations  Session  SessionFactory  Configuration  Transaction interface  Query  Criteria 39
  • 40. Session  Primary interface used by Hibernate applications to create, read and delete operations for instances of mapped entity classes  This instance wraps the JDBC connection and is also used to create transactions.  The persistent objects (POJO) are associated with exactly one Session. Once the Session is closed, they are detached.  Instance is created using SessionFactory Session openSession() throws HibernateException  Session instance is not thread-safe.  Instead each thread/transaction should obtain its own instance from a SessionFactory. 40
  • 41. SessionFactory  The application obtains Session instances from a SessionFactory interface  The SessionFactory is created from an configuration object. SessionFactory sf=cfg.buildSessionFactory();  The SessionFactory is an expensive object to create  It too is created at application start-up time  It should be created once and kept for latter use.  The SessionFactory object is used by all the threads of an applications  it is thread safe object  one SessionFactory object is created per database (where connecting to multiple sessions)  The SessionFactory is used to create Session Objects
  • 42. Session  The Session object is created from the SessionFactory object Session session=sf.openSession();  A Session object is lightweight and inexpensive to create.  Session object provides the main interface to accomplish work with the database  Does the work of getting a physical connection to the database (hopefully from a connection pool)  Session objects are not thread safe  Session objects shold not be kept open for a long time  Application create and destroy these as needed. Typically they are created to complete a single unit of work.  When modifications are made to the database, session objects are used to create a transaction object
  • 43. Transaction  Transaction objects are obtained from the session objects, when an modification to the database is needed. Transaction transaction =session.beginTransaction();  The Transaction object provides abstraction for the underlying implementation  Hibernate with use whatever transaction implementation is available (JDBC, JTA etc.)  It is optional; allowing developer to use their own transactional infrastructure.  Transaction objects should be kept open for a short time.
  • 44. How hibernate actually works with JDBC?  By default Hibernate creates a proxy for each of the entity class in mapping file. This class contain the code to invoke JDBC.  Proxies are created dynamically by sub-classing the entity object at runtime.  The subclass has all the methods of the parent, so when a method on the entity object is called, the proxy loads up the data from the database and calls the method. 44
  • 45. Persistent object states  The POJO or persistent objects can be in one of the three states is defined in relation to a persistence context (that means it is loaded into the Hibernate Session object)  Transient  Persistent  Detached 45
  • 46. Transient state  The instance is not associated with any persistence context. It has no persistent identity or primary key value.  Transient instances may be made persistent by calling save(), persist() or saveOrUpdate() of Session  Serializable save(Object object) throws HibernateException  Persist the given transient instance, first assigning a generated identifier or using the current value of the identifier property if the assigned generator is used. This operation cascades to associated instances if the association is mapped with cascade="save-update". 46
  • 47. void saveOrUpdate(Object object) throws HibernateException  Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks.  This operation cascades to associated instances if the association is mapped with cascade="save-update”  void persist(Object object) throws HibernateException  Make a transient instance persistent. This operation cascades to associated instances if the association is mapped with cascade="persist". 47
  • 48. Why do we have 2 methods – save() and persist()?  With save() the insert statement is executed immediately regardless of transaction state. It returns the inserted key so you can do something like this: long newKey = session.save(myObj); So use save() if you need an identifier assigned to the persistent instance immediately.  With persist(), the insert statement is executed in a transaction, not necessarily immediately.This is preferable in most cases. Use persist() if you don't need the insert to happen out-of-sequence with the transaction and you don't need the inserted key returned. 48
  • 49. get() and load()  The instance is currently associated with a persistence context. It has a persistent identity (primary key value) and can have a corresponding row in the database.  Any instance returned by a get() or load() method is persistent. Persistent instances may be made transient by calling delete().  Object get(Class clazz, Serializable id) throws HibernateException  Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.  If the instance is already associated with the session, return that instance. This method never returns an uninitialized instance.  Object load(Class clazz, Serializable id) throws HibernateException  Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed. 49
  • 50. get() and load() Difference between the load() and get() methods is that while load() assumes that instance exists and may return a proxy and we cannot guarantee that instance actually exists in the database (it may have got deleted). Hence get() must be used instead of load() to determine if an instance exists. Also note that the load() will throw an exception if instance does not exist. 50
  • 51. Detached  The instance was once associated with a persistence context/session but not is not attached to it may because the session was closed.  It has a persistent identity and can have a corresponding row in the database.  While for persistent instance Hibernate guarantees the relationship between persistent (database ) identity and Java identity for detached instance there is no such guarantees .  Detached instances may be made persistent by calling update(), saveOrUpdate()  The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge(). 51
  • 52. update()and merge()  void update(Object object) throws HibernateException  Update the persistent instance with the identifier of the given detached instance.  If there is a persistent instance with the same identifier, an exception is thrown.  This operation cascades to associated instances if the association is mapped with cascade="save-update".  Object merge(Object object) throws HibernateException  Copy the state of the given object onto the persistent object with the same identifier.  This operation cascades to associated instances if the association is mapped with cascade="merge". 52
  • 53. Example: update(),merge()differences To understand difference bw update() and merge() lets try to update values inserted earlier in servlet… 53
  • 54. When session2.update(c); is used instead of session2.merge(c); This is if there is a persistent instance with the same identifier in the session, update() will throw an exception! When session2.merge(c); is used the data gets updated because the instance that the session is holding gets its values from the detached instance when merge is called on detached instance. 54
  • 55. delete() and refresh()  void delete(Object object) throws HibernateException  Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to associated instances if the association is mapped with cascade="delete".  void refresh(Object object) throws HibernateException This method is useful to sync the state of the given instance with underlying database in cases where there are chances that database might have been altered outside the application as a result of a trigger. This is also useful in cases where direct SQL is used by the application to update the data. 55
  • 56. flush(), close(), clear  void flush() throws HibernateException  This is the method that actually synchronizing the underlying database with persistent object held in session memory.  This should be called at the end of a unit of work, before committing the transaction and closing the session (depending on flush-mode, Transaction.commit() calls this method).  Connection close() throws HibernateException  End the session by releasing the JDBC connection and cleaning up. It is not strictly necessary to close the session but at least it must be disconnected by calling disconnect()  void clear()  Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults. 56
  • 57. Transaction  Transaction can be set using the methods of Session. Transaction beginTransaction() throws HibernateException If Transaction object is associated with the session return that else create a new transaction. Transaction getTransaction() Get the Transaction instance associated with this session In both the cases the class of the returned Transaction object is determined by the property hibernate.transaction_factory.  The most common approach is the session-per-request pattern where a single Hibernate Session has same scope as a single database transaction.  The Hibernate session can be used for multiple DB operations (save, query, update) within the same request. 57
  • 58. Transaction methods  void begin() throws HibernateException  void commit() throws HibernateException  void rollback() throws HibernateException  To check if transactions was committed or rolled-back properly  boolean wasRolledBack() throws HibernateException  boolean wasCommitted() throws HibernateException  Code snippet wrapping statements in a transactions: Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } 58
  • 59. Mapping file  We have seen in the last session that the mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.  All persistent entity classes go in between the <hibernate-mapping> tags, include a class element.  We have also looked at how to map the bean columns to the database tables. By default, no properties of the class are considered persistent unless it is specified in hbm.  Some more important things to know about the mapping files are  Key generation  Relationship management  Lazy loading 59
  • 60. Hibernate persistent class  Class should have a constructor with no arguments.  Implementing Serializable is not compulsory. However if the object in to be stored in HttpSession then it will be necessary to make the persistent class Serializable.  The instance variables of the class represent attributes of business entity. The accessor and mutator methods needs to be provided for them as laid out by JavaBean specification guidelines. However the methods may not be declared public.  Can contain some business methods also. 60
  • 61. Accessor code  Hibernate uses accessor methods to populate the state of an object when loading the object from the database.  But what if we have a validation code in the setter method and we don’t want hibernate to run this validation while loading the data?  In that case, we need to tell Hibernate to directly access the instance variables  <property name=“fname“ type=“java.lang.String" column=“fname” access="field”> forcing Hibernate to bypass the setter method and access the instance variable directly. 61
  • 62. Key generation <id name="id" type="long" column="ID" > <generator class="native"/> </id> There is an alternative <composite-id> declaration that allows access to legacy data with composite keys. Its use is strongly discouraged for anything else.  The optional <generator> child element names a Java class used to generate unique identifiers for instances of the persistent class.  Hibernate provides a range of built-in key implementations. 62
  • 63. List of Key generator Generator Description It generates identifiers of type long, short or int that are unique only when no other process is increment inserting data into the same table. It should not the used in the clustered environment. It supports identity columns in DB2, MySQL, MS identity SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int. The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a sequence generator in Interbase. The returned identifier is of type long, short or int lets the application to assign an identifier to the assigned object before save() is called. This is the default strategy if no <generator> element is specified. 63
  • 64. Generator Description The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi hilo respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection. The seqhilo generator uses a hi/lo algorithm to efficiently seqhilo generate identifiers of type long, short or int, given a named database sequence. The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network uuid (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32. It uses a database-generated GUID string on MS SQL guid Server and MySQL. It picks identity, sequence or hilo depending upon the native capabilities of the underlying database. 64
  • 65. Generator Description retrieves a primary key assigned by a database trigger by select selecting the row by some unique key and retrieving the primary key value uses the identifier of another associated object. Usually used foreign in conjunction with a <one-to-one> primary key association. 65
  • 67. Object as Component  Associated Objects: <Components>  Set of Associated objects: <Composite-element>  Components can not exist their own, they share id of the parent entity  Lets say Person have many attributes and we want to divide it into two POJO such as Person and Address  Steps: 1. Put reference of Address in Person class 2. Map it into mapping file..as shown on next slide.
  • 69. Set as Composition  Just remove <component.../> with <set name="addresses"> <key column="aid"></key> <composite-element class="Address"> <property name="city"></property> <property name="state"></property> </composite-element> </set> Why? <key column="aid“/> As one person has N address, we need to have seperate table with name addresses and fk =aid that is same as id of person
  • 71. Inheritance  Super class & sub class relationship  Types of inheritance relations in hibernate 1. Table per concrete class <union-subclass> 2. Table per class hierarchy <subclass> 3. Table per class <joined-subclass>
  • 72. Inheritance Table per concrete class <union-subclass>  here Account class is an abstract class. So no need to map it to database, the values mapped by sub classes ie SavingAccount and CurrentAccount  Two separate table is going to be created for SavingAccount and CurrentAccount  Table per class hierarchy <subclass>  Here only one table is going to be created, all fields mapped to single table.  Not very memory efficient, May be faster  Table per class <joined-subclass>  Separate table mapped to all classes in the hierarchy
  • 73. Table per concrete class <union-subclass>
  • 74. Table per class hierarchy <subclass>  Single table is created per hierarchy ie some columns of table may contain nulls.
  • 75. Table per class <joined-subclass>  Table for each class is going to be created.
  • 76. Composite primary key  ore then one field combined as primray key. let consider public class Person { private int id; private String name; private String city; private String country;  lets say we want to consider id +name as composite pk  better to create an seperate table for ID  Create an seperate class ie PersonID
  • 78. Hibernate Unidirectional Mapping One to many one to one many to one many to many
  • 79. One to many  For example one person has N addresses.
  • 80. One to many  cascase=“all”  If underlying person record is changed corresponding Address get effected.  Id act as FK in Address table.
  • 81. one to one  For example one person can have only one addresses.
  • 82. Many to One  Many person can have same address.
  • 84. many to many  Many persons can have may address  We need third table to map these two tables.
  • 86. Hibernate Bidirectional mapping One to many one to one many to one many to many
  • 87. One to many bi-directional  In bi-directional mapping we can navigate from both the directions.  i.e. One can navigate from Person to Address and vice versa.
  • 90. HQL  Hibernate Query Language (HQL) is used similarly to SQL  The main difference between is HQL uses class name instead of table name, and property names instead of column name.  HQL is extremely simple to learn and use, and the code is always self- explanatory  We have already used HQL if you remember:
  • 92. HQL Delete Query Example
  • 93. HQL Insert Query Example  In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES.  HQL only support insert from another table. For example
  • 95. Native SQL queries  In Hibernate, HQL or criteria queries should be able to let you to execute almost any SQL query you want.  However, many developers are complaint about the Hibernates generated SQL statement is slow and more prefer to generated their own SQL (native SQL) statement
  • 96. Native SQL queries example  Hibernate provide a createSQLQuery method to let you call your native SQL statement directly.  In this example, you tell Hibernate to return you a Stock.class, all the select data (*) will match to your Stock.class properties automatically.  In this example, Hibernate will return you an Object array
  • 98. Named queries  Sometimes embedding HQL into Java code may make it harder to maintain.  Named query helps to externalize any HQL queries that can be statically defined.  The queries are specified in mapping document with the a name.  They are retrieved using method defined in the Session Query getNamedQuery(String queryName) throws HibernateException  Parameterized queries can also be written in the mapping document and this can be retrieved using the same approach that we discussed in the previous slide. 98
  • 99. Example: Named queries customer.hbm.xml <hibernate-mapping> … <query name="getCustID">select c.id from Customer as c where c.email=?</query> </hibernate-mapping> In the main //get Session etc Query q = session.getNamedQuery("getCustID"); q.setString(0,"mm@yahoo.com"); List id = q.list(); out.println("ID is : " + id.get(0)); ……. …….. 99
  • 100. Named Parameters  One of the shortcomings of parameterized query setting properties using index is that if the index value changes then the query will not be correct.  The Query’s method Query setXXX(String nm,XXX x) allows using named parameters instead of index values. In customer.hbm.xml <query name="getCustID">select c.id from Customer as c where c.email=:email</query> In the main Query q = session.getNamedQuery("getCustID"); q.setString("email","mm@yahoo.com"); List id = q.list(); out.println("ID is : " + id.get(0)); 100