Professional Documents
Culture Documents
Hibernate by Example: About The Speaker
Hibernate by Example: About The Speaker
Eitan Suez,
UptoData Inc
Java Programmer
http://u2d.com/
Weblog on http://java.net/
NFJS Speaker
Goals
Motivation
Style
Do {
Disclaimer
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?
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
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
Model-Centric
Coding a
Sample Application
(live)
The Model
Components
Many-One
One-Many
Default in hibernate v3
Hibernate implements lazy loading by
providing its own Collection API interface
implementations.
One-Many
(continued)
Example:
<bagname="pmts">
<keycolumn="invoice_id"/>
<onetomanyclass="com.u2d.nfjs.Payment"/>
</bag>
Many-Many
Example:
<bagname="actors"table="Movie_Actor">
<keycolumn="movies_id"/>
<manytomanycolumn="actors_id"
class="com.u2d.movielib.Actor"/>
</bag>
Inheritance
Four Strategies:
Implicit Polymorphism
2. The API
Basic Usage
Queries
Criteria API
Basic Usage
Primary Types are:
SessionFactory
Session
Query
Criteria
Prototypical SessionFactory
Configuration
publicclassHBMUtil{
Configurationcfg;SessionFactoryfactory;
publicHBMUtil()
{
cfg=newConfiguration();
cfg.addClass(Customer.class);
cfg.addClass(Invoice.class);
//...
cfg.setProperty(
Environment.CURRENT_SESSION_CONTEXT_CLASS,
"thread");
factory=cfg.buildSessionFactory();
}
...
Prototypical Session
Interaction
Sessions=factory.getCurrentSession();
s.beginTransaction();
//interactwithsessioninthis"pseudo"block
//forexample:
Customerc=newCustomer("Eitan");
c.setAccountNo(12345);
s.save(c);
s.getTransaction().commit();
Criteria API
object-oriented
powerful object model for constructing and
executing queries
Query by Example
Not locked in: can perform SQL
queries, including stored procedure
invocations
HQL
Criteria Queries
3. Session Strategies
4. Performance
5. Batch Processing
Batch Processing
Example:
Transactiontx=session.beginTransaction();
inti=0;
List<Widget>lotsOfWidgets=loadLotsOfWidgets();
for(Widgetwidget:lotsOfWidgets)
{
session.save(widget);
if(((i++)%20)==0)
{
s.flush();
s.clear();
}
}
session.getTransaction().commit();
6. UserType
Alternatively..
..
<typedefname="spantype"
class="com.u2d.persist.type.TimeSpanUserType"/>
<propertyname="timeSpan"type="spantype">
..
7. Annotations
Sample
Annotated
Class
8. Tools
Ant Tools
Eclipse Plug-ins
Mapping Editor
Filters
Filters
The Code..
The Output
Interesting Observations
Conclusions
References
http://www.hibernate.org/
Hibernate (Iverson)
Contact Information
Eitan Suez
http://u2d.com/
email: eitan@u2d.com