0% found this document useful (0 votes)
198 views

Hibernate

Hibernate is a popular open-source ORM (object-relational mapping) framework for Java and .NET. It allows developers to map object-oriented domain models to relational databases in a transparent way. Hibernate supports features like transparent lazy loading, query languages like HQL, criteria queries, and mapping of associations and inheritance hierarchies. It provides a clean and expressive API for persisting data in a relational database using POJOs.

Uploaded by

api-27399718
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views

Hibernate

Hibernate is a popular open-source ORM (object-relational mapping) framework for Java and .NET. It allows developers to map object-oriented domain models to relational databases in a transparent way. Hibernate supports features like transparent lazy loading, query languages like HQL, criteria queries, and mapping of associations and inheritance hierarchies. It provides a clean and expressive API for persisting data in a relational database using POJOs.

Uploaded by

api-27399718
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Hibernate

 Slides based on Gavin Kings


presentation at JAOO 2003
Hibernate
 Relational Persistence for Java and
.NET
 Opensource (LGPL)
 Mature and popular
 Custom API
Auction Object Model
Persistent Class
Default
public class AuctionItem {

private Long _id;
constructor private Set _bids;
private Bid _successfulBid
 Get/set pairs private String _description;

 Collection public Long getId() {


return _id;
property is an }
interface type private void setId(Long id) {
_id = id;
 Identifier }
public String getDescription() {
property return _description;
}
public void setDescription(String desc) {
_description=desc;
}

}
XML Mapping
 Readable <class name=“AuctionItem” table=“AUCTION_ITEM”>
<id name=“id” column=“ITEM_ID”>
metadata <generator class=“native”/>
 Column / </id>
table <property name=“description” column=“DESCR”/>
mappings <many-to-one name=“successfulBid”
column=“SUCCESSFUL_BID_ID”/>
 Surrogate <set name=“bids”
key cascade=“all”
generation lazy=“true”>
strategy <key column=“ITEM_ID”/>
 Collection <one-to-many class=“Bid”/>
</set>
metadata </class>
 Fetching
strategies
Dirty Checking
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();
Transitive Persistence
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();
Detachment
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();
Transparent Lazy Fetching
AuctionItem item = (AuctionItem) session.get(ActionItem.class, itemId);

SELECT … FROM AUCTION_ITEM ITEM WHERE ITEM.ITEM_ID = ?

Iterator iter = item.getBids().iterate();

SELECT … FROM BID BID WHERE BID.ITEM_ID = ?

item.getSuccessfulBid().getAmount();

SELECT … FROM BID BID WHERE BID.BID_ID = ?


Hibernate Query Options
 Hibernate Query Language (HQL)
 “Minimal” OO dialect of ANSI SQL
 Criteria Queries
 Extensible framework for expressing query
criteria as objects
 Includes “query by example”
 Native SQL Queries
Hibernate Query Language
 Make SQL be object oriented
 Classes and properties instead of tables and columns
 Polymorphism
 Associations
 Much less verbose than SQL
 Full support for relational operations
 Inner/outer/full joins, cartesian products
 Projection
 Aggregation (max, avg) and grouping
 Ordering
 Subqueries
 SQL function calls
Hibernate Query Language
 HQL is a language for talking about “sets of
objects”
 It unifies relational operations with object
models
Hibernate Query Language
Simplest HQL Query:

from AuctionItem

i.e. get all the AuctionItems:

List allAuctions = session.createQuery(“from AuctionItem”)


.list();
Hibernate Query Language
More realistic example:

select item
from AuctionItem item
join item.bids bid
where item.description like ‘hib%’
and bid.amount > 100

i.e. get all the AuctionItems with a Bid worth > 100 and
description that begins with “hib”
Hibernate Query Language
Projection:

select item.description, bid.amount


from AuctionItem item
join item.bids bid
where bid.amount > 100
order by bid.amount desc

i.e. get the description and amount for all the AuctionItems
with a Bid worth > 100
Hibernate Query Language
Aggregation:

select max(bid.amount), count(bid)


from AuctionItem item
left join item.bids bid
group by item.type
order by max(bid.amount)
Hibernate Info
 http://hibernate.org
 Hibernate in Action (Manning, 2004)
 Tool support
 http://xdoclet.sf.net
 http://boss.bekk.no/boss/middlegen

 http://www.andromda.org/

 http://www.hibernate.org/255.html
Misc

 Be sure to select the right strategy


for auto generating the primary key
 Automatically recreating database
from schema does not work if new
schema violates old foreign key
constraints

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy