Hibernate Presentation
Hibernate Presentation
Agenda
What is Hibernate
Spring Integration
Benefits Example
What is Hibernate?
Popular Open Source Object/Relational Mapping (ORM) tool Transparent persistence for POJOs (Plain Old Java Objects) Core of JBoss CMP 2.0 impl.
Features
Mapping from Java classes to database tables (and from Java data types to SQL data types). Data query and retrieval facilities. Generates the SQL calls and relieves the developer from manual result set handling and object conversion Portable to all data bases Provides persistence for Plain Old Java Objects(POJO)
Features
Support for detached persistent objects Powerful yet Simple queries using HQL.
Hibernate
Hibernate generates query called HQL Data base independent Supports OO concepts like, Inheritance and Polymorphic mappings HQL is gives optimal query and gives more performance
Hibernate
y No specific container needed y Hibernate components are
run the EJB s y EJB s are distributed components y EJB s can be invoked remotely
Why Hibernate?
Retains natural object model (transparent) Minimizes Code Does not require a container Model is not tied to persistence implementation
Searching, sorting
Sharing
Integrity
No polymorphism Fine grained models are difficult Stored procedures. (arguable, I suppose)
Business logic
Distribution
Data is important
Even so, the relational model is important The data will be around much longer than the Java application
The Goal
Take advantage of those things that relational databases do well Without leaving the language of objects / classes
</hibernate-mapping>
Object/Relational Mapping
JavaObject
int id; String name; String getName() int getId() void setName(String) void setId(int)
Commercial
TopLink
Hibernate's Goal
Remove 95% of common data persistence problems
Person.java
person.hbm l .xm
person table
Nam e.java
cats
table
XML Document
Persistent 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
<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>
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();
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 = ?
public List getItems() throws { return getSession() .createQuery(from AuctionItem item where item.type = :itemType)
item.setDescription(newDescription);
Transitive Persistence
Session session = sf.openSession(); Transaction tx = session.beginTransaction(); AuctionItem item = (AuctionItem) session.get(ActionItem.class, itemId); tx.commit(); session.close(); Bid bid = new Bid(); bid.setAmount(bidAmount); bid.setItem(item); item.getBids().add(bid); Session session2 = sf.openSession(); Transaction tx = session2.beginTransaction(); session2.update(item); tx.commit(); session2.close();
Version property (if there is one) Identifier value e.g. unsaved-value=0 (only works for generated surrogate keys, not for natural keys in legacy data) Write your own strategy, implement Interceptor.isUnsaved()
2.
3.
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
Benefits
Resource Management
IoC / AOP Session Management
HibernateTemplate
Simplifies Hibernate API
Spring IoC
Session
setSession()
DAO
save()
POJO
Interceptor
DAO
save(Cat)
Session
Spring Framework
Springs 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); }
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
THANK YOU