Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Presented by
Ryan Bohn and John Stein
Versatile, Inc.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
• Hibernate basics
– The Session
– Operations:
• load, save, delete
• Hibernate queries
– HQL
– Criteria queries
– Query by example
– Native queries
• Hibernate and Transactions
• Lazy loading
• Alternatives to Hibernate
• Q&A
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
• Modeling
– No polymorphism / inheritance
– No support for automatic conversion to objects
• Business logic
– There’s stored procedures, but:
• Very database specific
• Very coupled with the data, really belongs in the
application domain
• Transaction
– No concept of application level transactions
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
• Truly object-oriented
– Polymorphic associations and queries
– Three inheritance mapping strategies
From the data center to users on the move, Versatile turns data into knowledge.
• http://www.hibernate.org
– Download Jar
– Source code
– Javadoc
– Reference documentation
– Support forums
• Requirements
– JDK 1.4 or later
– Ehcache, commons logging, commons collections, asm,
dom4j, antlr, cglib
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
XML is yucky!
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
@Entity
@Table(name = “AUCTION_ITEM”)
public class AuctionItem {
private Long id;
private String description;
private Set<Bid> bids;
@Id
@Column(name = “ITEM_ID”, nullable = false)
public Long getId() {…}
@OneToMany(mappedBy = “item”)
public Set<Bid> getBids {…}
}
Example
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
• @Inheritence
– Three strategies
• Table per class
• Table per class hierarchy
• Joined subclass
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
from AuctionItem
From the data center to users on the move, Versatile turns data into knowledge.
select item
from AuctionItem item
join item.bids bid
where item.description like :desc
and bid.amount > 100
From the data center to users on the move, Versatile turns data into knowledge.
Projection:
select item.description,
bid.amount
from AuctionItem item
join item.bids bid
where bid.amount > 100
order by bid.amount desc
From the data center to users on the move, Versatile turns data into knowledge.
Aggregation:
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
List auctionItems =
session.createCriteria(AuctionItem.class)
.setFetchMode(“bids”, FetchMode.EAGER)
.add( Expression.like(“description”, description)
)
.createCriteria(“successfulBid”)
.add( Expression.gt(“amount”, minAmount) )
.list();
Equivalent HQL:
From the data center to users on the move, Versatile turns data into knowledge.
• Query By Example:
AuctionItem item = new AuctionItem();
item.setDescription(“hib”);
Bid bid = new Bid();
bid.setAmount(1.0);
List auctionItems =
session.createCriteria(AuctionItem.class)
.add( Example.create(item).enableLike(MatchM
ode.START) )
.createCriteria(“bids”)
.add( Example.create(bid) )
.list();
EXAMPLE
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
try {
AuctionItem item =
(AuctionItem) session.get(ActionItem.class, itemId);
bid.setItem(item);
item.getBids().add(bid);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
} finally {
session.close();
}
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.
• Oracle Toplink
• Ibatis
• JDO
• Many more
From the data center to users on the move, Versatile turns data into knowledge.
From the data center to users on the move, Versatile turns data into knowledge.