1 / 27

An Introduction to Hibernate

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

kalin
Download Presentation

An Introduction to Hibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matt Secoske secoskem@gmail.com An Introduction to Hibernate

  2. Agenda • What is Hibernate • Overview • Benefits • Example • Spring Integration • Benefits • Example • Questions

  3. 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.

  4. 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

  5. Other ORM Solutions • “Open” • iBatis SQL Maps • JDO • Commercial • TopLink

  6. Why Hibernate? • Retains natural object model (transparent) • Minimizes Code • Does not require a container • Model is not tied to persistance implementation

  7. Hibernate's Goal • Remove 95% of common data persistence problems

  8. How do you use it? • Act on your data model • Query in SQL and/or HQL • ... or using your object model

  9. Auction Object Model

  10. 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

  11. 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

  12. 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();

  13. 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();

  14. 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();

  15. 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();

  16. 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

  17. Integrating Spring and Hibernate

  18. Spring and Hibernate • Benefits • Examples

  19. 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

  20. Session setSession() DAO save() POJO Spring IoC

  21. Spring Interceptors (AOP) Business Logic Interceptor DAO getCatById() save(Cat) Session Spring Framework

  22. 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); }

  23. 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

  24. 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

  25. Questions • ???

More Related