Download presentation
Presentation is loading. Please wait.
1
An Introduction to Hibernate Matt Secoske secoskem@gmail.com
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
Object/Relational Mapping JavaObject int id; String name; String getName() int getId() void setName(String) void setId(int) SQL Table id [int] primary key, name [varchar(50)] Magic Happens Here (O/R Mapper – i.e. Hibernate)
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
10
How do you use it? Act on your data model Query in SQL and/or HQL... or using your object model
11
Auction Object Model
12
Persistant 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; } … }
13
XML Mapping Readable metadata Column/Table map Key Generation Collections Fetching Strategies <set name=“bids” cascade=“all” lazy=“true”>
14
Working with Data Session session = sessionFactory.openSession(); Transaction tx = s.beginTransaction(); AuctionItem item = (AuctionItem) session.get(ActionItem.class, itemId); item.setDescription(newDescription); tx.commit(); session.close(); Retrieve an AuctionItem and change description
15
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();
16
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();
17
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();
18
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
19
Integrating Spring and Hibernate
20
Spring and Hibernate ● Benefits ● Examples
21
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
22
Spring IoC Session POJO DAO setSession() save()
23
Spring Interceptors (AOP) Business Logic getCatById() save(Cat) DAOInterceptor Session Spring Framework
24
Spring’s HibernateTemplate Hibernate Only: HibernateTemplate: 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(); } } public User getUserByEmailAddress(final String email) { List list = getHibernateTemplate().find( "from User user where user.email=?", email, Hibernate.STRING); return (User) list.get(0); }
25
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
26
Important Links ● http://www.hibernate.org http://www.hibernate.org ● http://www.springframework.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.pdfhttp://www.hibernate.org/hib_docs/reference/en/pdf/hibernate_reference.pdf - Manual http://www.hibernate.org/hib_docs/online/jaoo_presentation/HibernateJAOO.ppthttp://www.hibernate.org/hib_docs/online/jaoo_presentation/HibernateJAOO.ppt - Presentation by Gavin King http://www.hibernate.org/200.htmlhttp://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
27
Questions ● ???
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.