Component Based Development Advanced part - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Component Based Development Advanced part

Description:

(based on Chistian Bauer's tutorial Hibernate Team) Overview. Persistence ... generator class='native'/ /id property name='description' column='DESCR' ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 20
Provided by: diku
Category:

less

Transcript and Presenter's Notes

Title: Component Based Development Advanced part


1
Component Based DevelopmentAdvanced part
  • Persistence using Hibernate
  • By Robert Bialek
  • 2005.05.18
  • (based on Chistian Bauer's tutorial Hibernate
    Team)

2
Overview
  • Persistence
  • Object-Relational Mapping (ORM)
  • Hibernate
  • Hibernate - Example
  • Supported Query Languages

3
Complexity of persistence
  • Mapping object model(OM) to relational schema(RS)
  • Relational model one-many, many-many relations
  • Object model association, aggregation,
    composition, multiplicity and inheritence
  • Keeping OM and RS in sync
  • Keeping SQL up-to-date when the models change
  • Querying data where to write the query code ?
  • Performance SQL for every operation?

4
Goal Transparent Persistence
  • Any class can be a persistent class
  • No interfaces have to be implemented
  • No persistent superclass has to be extended
  • Persistent classes can be used outside of the
    persistence context (Unit Tests)
  • Full portability without any dependency

5
Solution Object-Relational Mapping (ORM)
  • Advantages
  • Structural mapping more robust
  • Less error-prone code
  • Optimized performance all the time
  • Vendor independence
  • However
  • Relational model is important
  • Always ensure data integrity using database
  • The data in the database will be around much
    longer than the application

6
Hibernate what is it?
  • Popular Open Source ORM framework
  • It is not abstract, high level persistence
    framework like JDO or EJB
  • It doesn't try to abstract away complexity to a
    generic persistence framework
  • Instead, it specializes only in transparent
    persistence of objects in relational database
  • Works with many different databases
  • Relies on SQL databases while staying in Java OO
    paradigm
  • EJB 3.0 - Hibernate implements the persistence
    API and query language defined by EJB 3.0

7
Hibernate - features
  • Persistence for POJOs (Plain Old Java Objects)
  • Flexible and intuitive mapping
  • Support for fine-grained object models
  • Powerful, high performance queries
  • Dual-Layer Caching Architecture (HDLCA)
  • Retrieve objects by OID or using a query (HQL,
    QBE, SQL)

8
Hibernate requirements for a persistent class
  • JavaBean specification (or POJOs)
  • No-arg constructor
  • Accessor methods for properties
  • Collection property is an interface
  • Identifier property

9
Hibernate - Example
10
Hibernate XML MappingAuctionItem.hbm.xml to
reside at the same location as the compiled class
(or .jar)
  • lthibernate-mappinggt
  • ltclass nameAuctionItem table"AUCTION_ITEM"gt
  • ltid name"id" column"ITEM_ID"gt
  • ltgenerator class"native"/gt
  • lt/idgt
  • ltproperty name"description" column"DESCR"/gt
  • ltmany-to-one name"successfulBid"
  • column"SUCCESSFUL_BID_ID"/gt
  • ltset name"bids" cascade"all" lazy"true"gt
  • ltkey column"ITEM_ID"/gt
  • ltone-to-many class"Bid"/gt
  • lt/setgt
  • lt/classgt
  • lthibernate-mappinggt

11
Hibernate Mapping Configuration
  • ltclassgt - mappings of a class and its elements
  • ltcomponentgt - dependent class (without primary
    key)
  • ltpropertygt - mappings of class' properties
  • ltidgt - mapping of the primary key

12
Hibernate Usage steps
  • // 1. Build an item
  • AuctionItem a new AuctionItem()
  • a.setName(Robert)
  • // 2. Fire up Hibernate
  • Configuration cfg new Configuration()
  • .addClass(AuctionItem.cla
    ss)
  • SessionFactory sf cfg.buildSessionFactor
    y()
  • // 3. Open Session
  • Session sess sf.openSession()
  • // 4. Save AuctionItem and close Session
  • Transaction t sess.beginTransaction()
  • sess.save(a)
  • t.commit()
  • sess.close()

13
Hibernate Use example 1
  • Retrieve an AuctionItem and change the
    description
  • Session session sessionFactory.openSession()
  • Transaction tx session.beginTransaction()
  • AuctionItem item
  • (AuctionItem) session.get(ActionItem.class,
    itemId)
  • item.setDescription(newDescription)
  • tx.commit()
  • session.close()

Get by primary key
14
Hibernate Use example 2
  • Retrieve an AuctionItem and create a new
    persistent Bid
  • Bid bid new Bid()
  • bid.setAmount(bidAmount)
  • Session session sessionFactory.openSession()
  • Transaction tx session.beginTransaction()
  • AuctionItem item
  • (AuctionItem) session.get(ActionItem.class,
    itemId)
  • bid.setItem(item)
  • item.getBids().add(bid) // (No managed
    associations)
  • tx.commit()
  • session.close()

15
Supported Query Languages
  • Hibernate Query Language (HQL)
  • minimal object-oriented dialect of ANSI SQL
  • Criteria Queries (QBC)
  • extensible framework for query objects
  • includes Query By Example (QBE)
  • Native SQL queries
  • direct pass through with automatic mapping
  • named SQL queries in meta-data

16
HQL Example
  • List allAuctions session.createQuery(
  • select item
  • from AuctionItem item
  • join item.bids as bid
  • where item.description like Hibernate
  • and bid.amount gt 100
  • ).list()
  • i.e. get all the AuctionItems with a Bid worth
    more
  • than 100 and an item description that begings
    with Hibernate

17
QBC Example
List auctionItems session.createCriteria(Auctio
nItem.class) .setFetchMode("bids",
FetchMode.EAGER) .add( Expression.like("descr
iption", Hibernate) ) .createCriteria("succ
essfulBid") .add( Expression.gt("amount",
100) ) .list() i.e. get all the
AuctionItems with a Bid worth more than 100 and
an item description that begings with Hibernate
18
Conclusion
  • Hibernate
  • simple to use and powerful ORM framework
  • no dependencies between the application and the
    database
  • Supports EJB and POJOs
  • Retrieving persistent objects based on
  • OID (primary key)
  • Various queries

19
Resources
  • ttp//www.hibernate.org/
  • ttp//www.systemmobile.com/articles/IntroductionTo
    Hibernate.html Introduction to hibernate
  • ttp//www.hibernate.org/159.html Presentations
Write a Comment
User Comments (0)
About PowerShow.com