Hibernate Configuration - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Hibernate Configuration

Description:

left outer join mother.kittens as offspr. 15. More on Select. Or as an actual typesafe Java object, ... left join mother.kittens as offspr ... – PowerPoint PPT presentation

Number of Views:364
Avg rating:3.0/5.0
Slides: 19
Provided by: abdu3
Category:

less

Transcript and Presenter's Notes

Title: Hibernate Configuration


1
Hibernate Configuration
  • Shahid Amlani
  • Abdul Basit
  • Hamdan Ahmed
  • Sukhmeet Toor

2
Overview
  • Hibernate Configuration File
  • Persistent Classes
  • O/R Mapping
  • Associations

3
Configuration
  • 2 ways to perform configuration
  • hibernate.cfg.xml
  • new Configuration().configure()
  • Direct Instantiation
  • By specifying .addClass(), .addProperty()
  • Ultimately interested in session
  • sessionFactory configuration().sessionFactory()
  • sessionFactory.getCurrentSession()

4
Auto Mapping
  • An alternative (sometimes better) way is to
    specify the mapped class, and let Hibernate find
    the mapping document for you
  • Configuration cfg new Configuration()
    .addClass(org.hibernate.auction.Item.class)
    .addClass(org.hibernate.auction.Bid.class)

5
Persistent Classes
  • Look like Java Beans.
  • 2 requirements
  • No argument constructor so that
    Constructor.newInstance() can be called
  • Unique identifier property
  • Each class has a corresponding hibernate xml file

6
O/R Mapping
  • For objects, ends with .hbm.xml for every
    persistent class
  • These can get very complex. Well just look at
    the top level

7
Associations
  • 4 types
  • Many-to-One
  • An ordinary association to another persistent
    class is declared using a many-to-one element.
    The relational model is a many-to-one
    association a foreign key in one table is
    referencing the primary key column(s) of the
    target table.
  • One-to-One
  • A one-to-one association to another persistent
    class is declared using a one-to-one element.

8
Collection Mappings
  • The Hibernate mapping element used for mapping a
    collection depends upon the type of the
    interface. For example, a element is used
    for mapping properties of type Set.
  • Apart from , there is also , ,
    , and mapping
    elements.

9
Associationscont
  • Many-to-Many
  • Any collection of values or many-to-many
    association requires a dedicated collection table
    with a foreign key column or columns, collection
    element column or columns and possibly an index
    column or columns.
  • One-to-Many
  • A one to many association links the tables of two
    classes via a foreign key, with no intervening
    collection table.

10
Hibernate Query Language (HQL)
  • Hibernate is equipped with an extremely powerful
    query language that (quite intentionally) looks
    very much like SQL.
  • Mainly differs from SQL by being object-oriented
    rather than Tuple based.

11
The basics
  • The simplest query
  • from eg.Cat
  • from Cat
  • from Cat as cat
  • from Cat cat
  • All are equivalent and return all persistent
    instances of class Cat

12
Joins
  • HQL supports all types of joins from SQL
  • Examples
  • from Formula, Parameter
  • from Cat as cat inner join cat.mate as mate
    left outer join cat.kittens as kitten
  • from Cat as cat left join cat.mate.kittens as
    kittens

13
The Select clause
  • The select clause picks which objects and
    properties to return in the query result set.
  • Examples
  • select matefrom Cat as cat inner join cat.mate
    as mate
  • select cust.name.firstName from Customer as cust

14
More on Select
  • Queries may return multiple objects and/or
    properties as an array of type Object
  • select mother, offspr, mate.name from
    DomesticCat as mother inner join mother.mate as
    mate left outer join mother.kittens as offspr
  • or as a List,
  • select new list(mother, offspr, mate.name) from
    DomesticCat as mother inner join mother.mate as
    mate left outer join mother.kittens as offspr

15
More on Select
  • Or as an actual typesafe Java object,
  • select new Family(mother,mate,offspr) from
    DomesticCat as mother join mother.mate as mate
    left join mother.kittens as offspr(assuming
    that the class Family has an appropriate
    constructor)
  • Provides a lot of flexibility in a single query

16
Where is Where?
  • The where clause allows you to narrow the list of
    instances returned
  • from Cat where name'Fritz'
  • from Cat as cat where cat.name'Fritz'
  • select foo from Foo foo, Bar bar where
    foo.startDate bar.date

17
Caveats
  • HQL is extremely powerful, but has limitations
  • Report generation
  • Simple HQL translates to really complicated SQL
    (can be a real MESS)

18
References
  • An excellent tutorial on Hibernate
  • http//www.hibernate.org/hib_docs/v3/reference/en/
    html/tutorial.html
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com