introduction to hibernate 2

Upload: gun4tone

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Introduction to Hibernate 2

    1/26

    Introduction to Hibernate2.0

  • 8/6/2019 Introduction to Hibernate 2

    2/26

    Presentation Overview

    Hibernate Overview

    Architecture

    Understanding Hibernate ORM Getting Started

    Building an Application

    Other useful stuffs

  • 8/6/2019 Introduction to Hibernate 2

    3/26

    Hibernate Overview

    HIBERNATE is an ORM ( Object-Relational-Mapping) technology.

    It is an Open-Source and free technology ,developed in SourceForge.net.

    Other ORM Tools:

    TopLink is one such tool , subsequentlyadopted by Oracle and so proprietary.

    Hibernate and OJB(Object-Relational-Bridge)from Apache are two well known ORM tools,open-source and free.

    JDO, also falls within the same category.

  • 8/6/2019 Introduction to Hibernate 2

    4/26

    Facts about Hibernate

    Open Source Java Object-RelationalMapping Tool.

    One step up from Direct JDBC access.

    Generates SQL ,Relieves manual ResultSet handling and object conversion, and

    Keeps application portable to all DB. Hibernate provides persistence as a

    service, rather than as a framework .

  • 8/6/2019 Introduction to Hibernate 2

    5/26

    Architecture

    High level Design

  • 8/6/2019 Introduction to Hibernate 2

    6/26

    Why ORM?

    The main features include :

    1. Less error-prone code2. Optimized performance all the time

    3. Solves portability issues4. Reduce development time

    The ultimate goal

    Take advantage of those things that relational databases

    do well , without leaving the Java language of objects /classes .I will say , the ultimate aim is - Do less work ,Happy DBA .

  • 8/6/2019 Introduction to Hibernate 2

    7/26

    Getting Started With Hibernate

    1. Download and Install Hibernate2. Hibernate is available for download at

    http://www.hibernate.org/

    3. Include the hibernate.jar file in the workingdirectory.

    4. Place your JDBC driver jar file in the lib

    directory.

    5. Edit hibernate configuration files, specifying

    values for your database (Hibernate will

    create a schema automatically)

  • 8/6/2019 Introduction to Hibernate 2

    8/26

    Rules for your class

    Must have Bean-style getXXX() and setXXX()

    Must have default constructor

    Not final

    Identifier recommended (not required) any primitive type

    any primitive wrapper

    String

    Date user-defined class (good for existing tables with

    composite keys)

  • 8/6/2019 Introduction to Hibernate 2

    9/26

    Creating the mapping file

    Defining a class

    Defining relationships

  • 8/6/2019 Introduction to Hibernate 2

    10/26

    class name=name.of.class

    table=name_of_table

    schema=database_schema

    proxy=name.of.proxy.class

    discrimator-value=val

    mutable=true

    polymorphism=implicit

    dynamic-update=false

    dynamic-insert=falsepersister=PersisterClass>

    ...

  • 8/6/2019 Introduction to Hibernate 2

    11/26

  • 8/6/2019 Introduction to Hibernate 2

    12/26

  • 8/6/2019 Introduction to Hibernate 2

    13/26

    Simple Association (one-to-one)

    One-to-one mapping

    Java usage:

    Bar myBar = Foo.getBar();

    Mapping file:

    ...

    Table Schema:

    Foo Bar

    id id

  • 8/6/2019 Introduction to Hibernate 2

    14/26

    Simple Reference (many-to-one)

    Java usage:

    Set myBars = Foo.getBars();

    Mapping file:

    ...

    Table Schema:

    Fooid bar_id

    Bar

    id

  • 8/6/2019 Introduction to Hibernate 2

    15/26

    Basic Collection (one-to-many)

    Java usage:

    Set Foo.getBars()

    Mapping file:

    ...

    Table schema:

    Fooid

    Bar

    id foo_id

  • 8/6/2019 Introduction to Hibernate 2

    16/26

    Many-to-many mapping

    Java usage:

    Set myBars = Foo.getBars();

    Mapping file:

    ...

    Schema:

    Foo

    id

    Bar

    id

    Foo_Bar

    foo_id bar_id

  • 8/6/2019 Introduction to Hibernate 2

    17/26

    Collection

    Java usage:

    Set myPeople = Foo.getPeople();

    Mapping file:

    ...

    Table Schema:

    FooId

    Person

    foo_id name

  • 8/6/2019 Introduction to Hibernate 2

    18/26

    Map

    Java usage:

    Map myAges = Foo.getAges();

    Mapping file:

    ...

    Table schema:Foo

    id

    Ages

    Id name age

  • 8/6/2019 Introduction to Hibernate 2

    19/26

    Hibernate.config.xml

    com.mysql.jdbc.Driver

    jdbc:mysql://localhost/hibernatetutorial

    root

    10

    false

    org.hibernate.dialect.MySQLDialect

    update

  • 8/6/2019 Introduction to Hibernate 2

    20/26

    Hibernate Query Language

    Hibernate is equipped with an extremely

    powerful query language that (quite

    intentionally) looks very much like SQL.

    But don't be fooled by the syntax; HQL is

    fully object-oriented, understanding

    notions like inheritence, polymorphism and

    association.

  • 8/6/2019 Introduction to Hibernate 2

    21/26

    Queries are case-insensitive, except for

    names of Java classes and properties. So SeLeCT is the same as sELEct is the

    same as SELECT but

    org.hibernate.eg.FOO is not

    org.hibernate.eg.Foo and foo.barSet is not

    foo.BARSET.

  • 8/6/2019 Introduction to Hibernate 2

    22/26

    Why to use HQL?

    Full support for relational operations: HQL allows representingSQL queries in the form of objects. Hibernate Query Language usesClasses and properties instead of tables and columns.

    Return result as Object: The HQL queries return the query

    result(s) in the form of object(s), which is easy to use. Thiselemenates the need of creating the object and populate the datafrom result set.

    Polymorphic Queries: HQL fully supports polymorphic queries.Polymorphic queries results the query results along with all the childobjects if any.

    Easy to Learn: Hibernate Queries are easy to learn and it can beeasily implemented in the applications.

  • 8/6/2019 Introduction to Hibernate 2

    23/26

    Understanding HQL Syntax

    Any Hibernate Query Language may consist

    of following elements:

    Clauses Aggregate functions

    Subqueries

  • 8/6/2019 Introduction to Hibernate 2

    24/26

    Clauses in the HQL are:

    from

    select

    where order by

    group by

  • 8/6/2019 Introduction to Hibernate 2

    25/26

  • 8/6/2019 Introduction to Hibernate 2

    26/26

    Thank You