An Introduction to Hibernate

batterycopperInternet and Web Development

Nov 12, 2013 (3 years and 11 months ago)

80 views

An Introduction to Hibernate



Matt Secoske

secoskem@gmail.com

Agenda


What is Hibernate


Overview


Benefits


Example


Spring Integration


Benefits


Example


Questions

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

int id;

String name;

String getName()

int getId()

void setName(String)

void setId(int)

SQL Table

id [int] primary key,

name [varchar(50)]

Magic Happens Here

(O/R Mapper


i.e. Hibernate)

Other ORM Solutions


“Open”


iBatis SQL Maps


JDO


Commercial


TopLink

Why Hibernate?


Retains natural object model
(transparent)


Minimizes Code


Does not require a container


Model is not tied to persistance
implementation

Hibernate's Goal


Remove 95% of common data
persistence problems


Person.java
Name.java
person.hbm.xml
person table
Hibernate Component Mapping
Example
Cat.java
cat.hbm.xml
cats table
Hibernate Collection Mapping
with recursion and subclassing
How do you use it?


Act on your data model


Query in SQL and/or HQL


... or using your object model


Auction Object Model

Persistant Class


Default
Constructor


Getters/Setters


Collections use
interface types


Identifier property

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;


}




}

XML Mapping


Readable
metadata


Column/Table
map


Key Generation


Collections


Fetching
Strategies

<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>

Working with Data

Session session = sessionFactory.openSession();

Transaction tx = s.beginTransaction();


AuctionItem item =


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

item.setDescription(newDescription);


tx.commit();

session.close();

Retrieve an
AuctionItem

and change description

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

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

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


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

Spring and Hibernate


Benefits


Examples

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

POJO

DAO

setSession()

save()

Spring Interceptors (AOP)

Business Logic

getCatById()

save(Cat)

DAO

Interceptor

Session

Spring Framework

Spring’s HibernateTemplate

Hibernate Only:

HibernateTemplate:

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
();

}

}

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)


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


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


???