0% found this document useful (0 votes)
228 views18 pages

Hibernate for Java and .NET Persistence

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)
228 views18 pages

Hibernate for Java and .NET Persistence

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 = [Link]();


Transaction tx = [Link]();

AuctionItem item =
(AuctionItem) [Link]([Link], itemId);
[Link](newDescription);

[Link]();
[Link]();
Transitive Persistence
Retrieve an AuctionItem and create a new persistent Bid

Bid bid = new Bid();


[Link](bidAmount);

Session session = [Link]();


Transaction tx = [Link]();

AuctionItem item =
(AuctionItem) [Link]([Link], itemId);
[Link](item);
[Link]().add(bid);

[Link]();
[Link]();
Detachment
Retrieve an AuctionItem and create a new persistent Bid

Session session = [Link]();


Transaction tx = [Link]();
AuctionItem item =
(AuctionItem) [Link]([Link], itemId);
[Link]();
[Link]();

[Link](newDescription);

Session session2 = [Link]();


Transaction tx = [Link]();
[Link](item);
[Link]();
[Link]();
Transparent Lazy Fetching
AuctionItem item = (AuctionItem) [Link]([Link], itemId);

SELECT … FROM AUCTION_ITEM ITEM WHERE ITEM.ITEM_ID = ?

Iterator iter = [Link]().iterate();

SELECT … FROM BID BID WHERE BID.ITEM_ID = ?

[Link]().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 = [Link](“from AuctionItem”)


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

select item
from AuctionItem item
join [Link] bid
where [Link] like ‘hib%’
and [Link] > 100

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

select [Link], [Link]


from AuctionItem item
join [Link] bid
where [Link] > 100
order by [Link] desc

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

select max([Link]), count(bid)


from AuctionItem item
left join [Link] bid
group by [Link]
order by max([Link])
Hibernate Info
 [Link]
 Hibernate in Action (Manning, 2004)
 Tool support
 [Link]
 [Link]

 [Link]

 [Link]
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