850 likes | 1.62k Views
Matt Secoske secoskem@gmail.com. An Introduction to Hibernate. Agenda. What is Hibernate Overview Benefits Example Spring Integration Benefits Example Questions. What is Hibernate?. Popular Open Source (LGPL) Object/Relational Mapping (ORM) tool
E N D
Matt Secoske secoskem@gmail.com An Introduction to Hibernate
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.
JavaObject SQL Table int id; String name; String getName() int getId() void setName(String) void setId(int) id [int] primary key, name [varchar(50)] Magic Happens Here (O/R Mapper – i.e. Hibernate) Object/Relational Mapping
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
How do you use it? • Act on your data model • Query in SQL and/or HQL • ... or using your object model
Persistant Class 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; } … } • Default Constructor • Getters/Setters • Collections use interface types • Identifier property
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> • Readable metadata • Column/Table map • Key Generation • Collections • Fetching Strategies
Working with Data 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();
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
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
Session setSession() DAO save() POJO Spring IoC
Spring Interceptors (AOP) Business Logic Interceptor DAO getCatById() save(Cat) Session Spring Framework
Spring’s 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); }
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 • ???