Hibernate With Example
Hibernate With Example
Eitan Suez,
UptoData Inc
Motivation
●
My experience using Hibernate has convinced me
that it has many gems, is useful in many
circumstances, and is worth studying
●
The belief that the best way to learn something is by
doing it actively
Style
Do {
●
Model a class of objects
●
Construct database mapping
●
Export or update database schema
●
Write Hibernate code to save sample data to
database
●
Write Hibernate code to query database
} until we've covered most of the mapping features of
Hibernate
Disclaimer
●
There is a lot to this framework, cannot cover every
aspect in a simple 1-2 hr course
●
Emphasis on constructing a meaningful sample
application at the expense of completeness: I will
not be covering every minute detail of the
framework
Agenda
1.Project Background
2.Mapping
3.The API
4.Session Usage Strategies
5.Performance
6.Batch Processing
7.UserType's
8.Annotations
9.Tools, Hibernate 3 features
What is Hibernate?
●
An Object/Relational Mapping (O/R M) API for Java
●
Open Source (LGPL)
●
Today a part of RedHat
●
Principal author: Gavin King
●
Other Major Figure: Christian Bauer
●
Almost a defacto standard O/R M for Java
●
Current version 3.1 (3.2 almost final)
Once upon a time..
1. A single mechanism for specifying Object-
Database Mapping:
●
hibernate .hbm.xml mapping files
2. One Specific Runtime API
Hibernate Today
●
Multiple Projects
●
Compliance with new EJB3
Persistence Standards
●
Supports both xml mapping
and Java 5 Annotations
●
Supports both the Hibernate
API and the EJB3 Persistence
API
1. Mapping
●
The process of specifying the bindings between an
object model and a database schema
●
Principal mechanism is via XML mapping files
●
Defacto file name extension: is .hbm.xml
●
Multiple ways to set this up: a single file, one file per
class. Best practice is is to use one file per class,
with each file placed next to its corresponding class
file in the package hierarchy, and loaded as a
resource
Mapping
●
Entities
●
Basic Properties
●
Components
●
Associations
●
Many-To-One
●
One-To-Many
●
Many-To-Many
●
Inheritance Mapping
●
Modeling with Interfaces
Model-Centric
●
Write Model Classes and Mappings;
Generate Database Schema
●
Reverse Engineering Tools available to do the
reverse
Coding a
Sample Application
(live)
The Model
Many-One
●
Example: Invoice references a single
customer. Other invoices may
reference that same customer.
●
Example mapping:
<manytoone name="customer"
column="customer_id" />
●
Example:
<bag name="pmts">
<key column="invoice_id"/>
<onetomany class="com.u2d.nfjs.Payment"/>
</bag>
Many-Many
●
Many-many associations are specified
using an extension of one-many.
●
Example:
<bag name="actors" table="Movie_Actor">
<key column="movies_id"/>
<manytomany column="actors_id"
class="com.u2d.movielib.Actor"/>
</bag>
Inheritance
●
Four Strategies:
●
Table per class hierarchy
●
Table per subclass
●
Table per concrete class using union-
subclass
●
Table per concrete class using implicit
polymorphism
Implicit Polymorphism
●
Personally a great fan of implicit polymorphism;
●
I find this mechanism gives me the freedom to
model using interfaces without complicating or
sacrificing persistence
●
many-to-one associations to polymorphic types
specified in mapping file using the <any> tag
●
many-to-many associations to polymorphic types
specified in mapping file using the <many-to-any>
tag
2. The API
●
Basic Usage
●
What Spring Offers
●
Queries
●
HQL (Hibernate Query Language)
●
Criteria API
Basic Usage
Primary Types are:
●
SessionFactory
●
Session
●
Query
●
Criteria
Basic Usage: SessionFactory
●
One per database
●
A factory for sessions
●
Container for JVM-level cache (second-
level cache)
Prototypical SessionFactory
Configuration
public class HBMUtil {
Configuration cfg; SessionFactory factory;
public HBMUtil()
{
cfg = new Configuration();
cfg.addClass(Customer.class);
cfg.addClass(Invoice.class);
// ...
cfg.setProperty(
Environment.CURRENT_SESSION_CONTEXT_CLASS,
"thread");
factory = cfg.buildSessionFactory();
}
...
Prototypical Session
Interaction
Session s = factory.getCurrentSession();
s.beginTransaction();
// interact with session in this "pseudo" block
// for example:
Customer c = new Customer("Eitan");
c.setAccountNo(12345);
s.save(c);
s.getTransaction().commit();
●
It refactors the use of Hibernate
●
Avoiding duplication of session and transaction
setup and teardown code
●
Provides various utility methods for common
usages
●
Provides two implementations:
●
HibernateTemplate / Callback Pattern
●
HibernateInterceptor (a Spring AOP
MethodInterceptor)
Spring for Hibernate Example
getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Customer c = new Customer("Eitan");
c.setAccountNo(12345);
s.save(c);
}
}
getHibernateTemplate().fetch("from Customer");
String hql = "from Customer c where c.age > :age";
Query q = session.createQuery();
q.setInteger("age", 33);
q.setFirstResult(20);
q.setMaxResults(10); // fetch the third page
List customers = q.list(hql);
Criteria Queries
●
What makes the Criteria API powerful is
that it allows queries to be specified by
composition.
●
This means that queries can be
constructed dynamically.
Criteria c = session.createCriteria(Customer.class);
c.add( Restrictions.ilike("name", "Albert%") );
c.addOrder( Order.asc("age") );
c.setMaxResults(20);
c.list();
// entire sequence of calls can also be chained,
// like so:
session.createCriteria(Customer.class).
add( Restrictions.ilike("name", "Albert%") ).
addOrder( Order.asc("age") ).
setMaxResults(20).
list();
3. Session Strategies
●
Session per request with detached objects
●
a new session is obtained for every request. any
objects needed in long conversations must be
attached to the new session
●
Open Session in View
●
session scope is extended to include view rendering
phase
●
Session per conversation
●
use same session, but disconnect from underlying
JDBC connection after committing a transaction
Batch Processing
●
Example:
Transaction tx = session.beginTransaction();
int i=0;
List<Widget> lotsOfWidgets = loadLotsOfWidgets();
for (Widget widget : lotsOfWidgets)
{
session.save(widget);
if ( ((i++) % 20) == 0)
{
s.flush();
s.clear();
}
}
session.getTransaction().commit();
6. UserType
●
Can provide your own serialization and
deserialization mechanisms for properties
1.Implement the UserType interface
2.Specify the property type in the mapping using
type="classname"
3.Alternatively can create alias for classname with
<typedef>
Alternatively..
..
<typedef name="spantype"
class="com.u2d.persist.type.TimeSpanUserType" />
<property name="timeSpan" type="spantype">
..
UserType Example: TimeSpan
public class TimeSpanUserType implements CompositeUserType
{
public Object nullSafeGet(java.sql.ResultSet rs, String[] names,
SessionImplementor session, Object owner) ..
{
Date from =
(Date) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]);
Date to = (Date) Hibernate.TIMESTAMP.nullSafeGet(rs, names[1]);
return new TimeSpan(from, to);
}
public void nullSafeSet(java.sql.PreparedStatement pstmt,
Object value, int index, SessionImplementor session)
{
TimeSpan span = (TimeSpan) value;
Hibernate.TIMESTAMP.nullSafeSet(pstmt, span.startDate(), index);
Hibernate.TIMESTAMP.nullSafeSet(pstmt, span.endDate(),
index + 1);
}
..
Sample
Annotated
Class
Sample Annotated Class
8. Tools
●
Ant Tools
●
DDL-Related: SchemaExport and SchemaUpdate
●
Eclipse Plug-ins
●
Console: HQL scratch pad
●
Mapping Editor
●
Hibernate Configuration File Generator Wizard
●
Reverse Engineering Wizards
●
Custom Hibernate Eclipse “Perspective”
Some Interesting Version 3
Features
●
Filters
●
XML Entity Mode
Filters
●
A simple mechanism to filter tables, similar to what
views provide, without having to specify the filter in
queries
●
Filter can be defined and named in the mapping file
●
Filter must be enabled programmatically on a per-
session basis with
session.enableFilter(filterName)
XML/DOM4J Entity Mode
●
A new, Experimental Feature in Hibernate 3
●
Very promising, potentially enabling powerful features
including import/export, SOAP, and XSLT-based
reporting
●
Consists of:
●
Adding XML data binding information to mapping files
●
The ability to define a specific entity mode to use when working
with a session (for XML, use the DOM4J entity mode)
●
Using the session API to bind database information directly to
XML, bypassing object model entirely; bi-directional.
Session domsession =
session.getSession(EntityMode.DOM4J);
The Code..
The Output
XML Mapping Information
Interesting Observations
●
Many O/R mapping solutions have been devised over
the years. Hibernate is probably the most
successful.
●
Effectively addresses major object mapping
problems head-on, giving us choices for modeling
inheritance, polymorphism
●
Flexible framework, can provide own
implementations for serializing properties
(UserType), how properties are accessed
(PropertyAccessor), and more
Conclusions
●
Hibernate is a mature, complete solution for
addressing Object/Relational mapping
●
It is an active project with a large community, large-
scale adoption, keeps up with (and has assisted in
redefining) Java Persistence Standards and evolution
●
Lots of tools: XDoclet / Ant / Eclipse Tooling
References
●
http://www.hibernate.org/
●
Hibernate In Action (Bauer & King)
●
Hibernate, a Developer's Notebook (Elliott)
●
Hibernate Quickly (Peak & Heudecker)
●
Hibernate (Iverson)
Contact Information
●
Eitan Suez
●
http://u2d.com/
●
email: [email protected]