Matt secoske secoskem@gmail com
This presentation is the property of its rightful owner.
Sponsored Links
1 / 27

An Introduction to Hibernate PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

Matt Secoske [email protected] An Introduction to Hibernate. Agenda. What is Hibernate Overview Benefits Example Spring Integration Benefits Example Questions. What is Hibernate?. Popular Open Source (LGPL) Object/Relational Mapping (ORM) tool

Download Presentation

An Introduction to Hibernate

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Matt secoske secoskem@gmail com

Matt Secoske

[email protected]

An Introduction to Hibernate


Agenda

Agenda

  • What is Hibernate

    • Overview

    • Benefits

    • Example

  • Spring Integration

    • Benefits

    • Example

  • Questions


What is hibernate

What is Hibernate?

  • Popular Open Source (LGPL) Object/Relational Mapping (ORM) tool

  • Transparent persistence for POJOs (Plain Old Java Objects)

  • Core of JBoss CMP 2.0 impl.


Object relational mapping

JavaObject

SQL Table

int id;

String name;

String getName()

int getId()

void setName(String)

void setId(int)

id [int] primary key,

name [varchar(50)]

Magic Happens Here

(O/R Mapper – i.e. Hibernate)

Object/Relational Mapping


Other orm solutions

Other ORM Solutions

  • “Open”

    • iBatis SQL Maps

    • JDO

  • Commercial

    • TopLink


Why hibernate

Why Hibernate?

  • Retains natural object model (transparent)

  • Minimizes Code

  • Does not require a container

  • Model is not tied to persistance implementation


Hibernate s goal

Hibernate's Goal

  • Remove 95% of common data persistence problems


How do you use it

How do you use it?

  • Act on your data model

  • Query in SQL and/or HQL

  • ... or using your object model


Auction object model

Auction Object Model


Persistant class

Persistant Class

public class AuctionItem {

private Long _id;

private Set _bids;

private Bid _successfulBid

private String _description;

public Long getId() {

return _id;

}

private void setId(Long id) {

_id = id;

}

public String getDescription() {

return _description;

}

public void setDescription(String desc) {

_description=desc;

}

}

  • Default Constructor

  • Getters/Setters

  • Collections use interface types

  • Identifier property


Xml mapping

XML Mapping

<class name=“AuctionItem” table=“AUCTION_ITEM”>

<id name=“id” column=“ITEM_ID”>

<generator class=“native”/>

</id>

<property name=“description” column=“DESCR”/>

<many-to-one name=“successfulBid”column=“SUCCESSFUL_BID_ID”/>

<set name=“bids”

cascade=“all”

lazy=“true”>

<key column=“ITEM_ID”/>

<one-to-many class=“Bid”/>

</set>

</class>

  • Readable metadata

  • Column/Table map

  • Key Generation

  • Collections

  • Fetching Strategies


Working with data

Working with Data

Retrieve an AuctionItem and change description

Session session = sessionFactory.openSession();

Transaction tx = s.beginTransaction();

AuctionItem item =

(AuctionItem) session.get(ActionItem.class, itemId);

item.setDescription(newDescription);

tx.commit();

session.close();


Working with data1

Working with Data

Retrieve an AuctionItem and create a new persistent Bid

Bid bid = new Bid();

bid.setAmount(bidAmount);

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

AuctionItem item =

(AuctionItem) session.get(ActionItem.class, itemId);

bid.setItem(item);

item.getBids().add(bid);

tx.commit();

session.close();


Hibernate in code

Hibernate in code

Retrieve an AuctionItem and create a new persistent Bid

Bid bid = new Bid();

bid.setAmount(bidAmount);

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

AuctionItem item =

(AuctionItem) session.get(ActionItem.class, itemId);

bid.setItem(item);

item.getBids().add(bid);

tx.commit();

session.close();


Hibernate in code1

Hibernate in code

Retrieve an AuctionItem and create a new persistent Bid

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

AuctionItem item =

(AuctionItem) session.get(ActionItem.class, itemId);

tx.commit();

session.close();

item.setDescription(newDescription);

Session session2 = sf.openSession();

Transaction tx = session2.beginTransaction();

session2.update(item);

tx.commit();

session2.close();


Benefits

Benefits

  • Metadata controlled persistence

  • Transparent - working with the model, not the data access technology

  • Pooling, Caching, Transactions can all be handled outside of our code


Integrating spring and hibernate

Integrating Spring and Hibernate


Spring and hibernate

Spring and Hibernate

  • Benefits

  • Examples


Benefits1

Benefits

  • Resource Management

    • IoC / AOP Session Management

  • Extended Transaction Support

    • JTA and/or JDBC

    • Flexible Transaction Demarcation

      • Programmatic

      • Declarative (Spring's XML config)

  • HibernateTemplate

    • Simplifies Hibernate API


Spring ioc

Session

setSession()

DAO

save()

POJO

Spring IoC


Spring interceptors aop

Spring Interceptors (AOP)

Business Logic

Interceptor

DAO

getCatById()

save(Cat)

Session

Spring Framework


Spring s hibernatetemplate

Spring’s HibernateTemplate

Hibernate Only:

public User getUserByEmailAddress(final String email) {try{Session session = sessionFactory.openSession();List list = session.find("from User user where user.email=?",

email,Hibernate.STRING);return (User) list.get(0);}finally{session.close();}

}

HibernateTemplate:

public User getUserByEmailAddress(final String email) {List list =getHibernateTemplate().find("from User user where user.email=?", email,Hibernate.STRING);return (User) list.get(0);

}


News of the day 12 20 2004

News of the day (12/20/2004)

  • Hibernate 3.0 Beta 1 released!

    • Features:

      • Virtualization (Filters, Permissions, etc)

      • New mapping constructs

      • HQL rewrite – using ANTLR

      • Representation Independence – i.e.HashMap

      • Statistics and JMX Monitoring

      • Stored Procedures / Handwritten SQL

      • … Much more


Important links

Important Links

  • http://www.hibernate.org

  • http://www.springframework.org

Parts of this presentation come from documentation and

presentations on the hibernate.org website:

http://www.hibernate.org/hib_docs/reference/en/pdf/hibernate_reference.pdf - Manual

http://www.hibernate.org/hib_docs/online/jaoo_presentation/HibernateJAOO.ppt - Presentation by Gavin King

http://www.hibernate.org/200.html - Road Map

Books:

J2EE Development without EJB – Rod Johnson and Juergen Hoeller

Better, Faster, Lighter Java – Bruce A. Tate and Justin Gehtland

Hibernate in Action – Christian Bauer and Gavin King


Questions

Questions

  • ???


  • Login