2. Facts about Hibernate
True transparent persistence
Query language aligned with SQL
Does not use byte code enhancement
Free/open source
3. What is Hibernate?
** Hibernate is a powerful, ultra-high performance object/relational
persistence and query service for Java. Hibernate lets you develop
persistent objects following common Java idiom - including association,
inheritance, polymorphism, composition and the Java collections
framework. Extremely fine-grained, richly typed object models are
possible. The Hibernate Query Language, designed as a quot;minimalquot;
object-oriented extension to SQL, provides an elegant bridge between
the object and relational worlds. Hibernate is now the most popular
ORM solution for Java.
** http://www.hibernate.org
5. Object/Relational
Mapping (ORM)
Mapping application objects to relational
database
Solution for infamous object-relational
impedance mismatch
Finally application can focus on objects
6. Transparent Persistence
Persist application objects without knowing
what relational database is the target
Persist application objects without
“flattening” code weaved in and out of
business logic
7. Query Service
Ability to retrieve sets of data based on
criteria
Aggregate operations like count, sum, min,
max, etc.
8. Why use Hibernate?
Simple to get up and running
Transparent Persistence achieved using
Reflection
Isn’t intrusive to the build/deploy process
Persistence objects can follow common java
idioms: Association, Inheritance,
Polymorphism, Composition, Collections
In most cases Java objects do not even know
they can be persisted
9. Why use Hibernate
cont
Java developer can focus on object modeling
Feels much more natural than Entity Beans or
JDBC coding
Query mechanism closely resembles SQL so
learning curve is low
10. What makes up a
Hibernate application?
Standard domain objects defined in Java as
POJO’s, nothing more.
Hibernate mapping file
Hibernate configuration file
Hibernate Runtime
Database
11. What is missing from a
Hibernate application?
Flattening logic in Java code to conform to
relational database design
Inflation logic to resurrect Java object from
persistent store
Database specific tweaks
12. Hibernate Classes
SessionFactory - One instance per app.
Creates Sessions. Consumer of hibernate
configuration file.
Session - Conversation between application
and Hibernate
Transaction Factory - Creates transactions to
be used by application
Transaction - Wraps transaction
implementation(JTA, JDBC)
13. Hibernate Sample App
Standard Struts Web Application
Deployed on JBoss
Persisted to MySQL database
All hand coded, didn’t use automated tools to
generate Java classes, DDL, or mapping files
Disclaimer: Made simple stupid on purpose
15. Separation of
Responsibility
Persistence
Struts
JSP
classes
Action
16. DVD POJO
NOTE:
public class DVD { I transform
private Long id; the DVDForm Bean to DVD
prior to persisting for
private String name; added flexibility
private String url;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
..
..
}
22. Hibernate.cfg.xml
Alternative to hibernate.properties
Handles bigger applications better
Bind SessionFactory to JNDI Naming
Allows you to remove code like the following
and put it in a configuration file
Configuration cfg = new Configuration().addClass(DVD.class);
24. Schema Generation
SchemaExport can generate or execute DDL
to generate the desired database schema
Can also update schema
Can be called via ant task
26. Mapping File Generation
MapGenerator - part of Hibernate extensions
Generates mapping file based on compiled
classes. Some rules apply.
Does repetitive grunt work
27. Links to live by
http://www.hibernate.org
http://www.springframework.org
http://www.jboss.org
http://raibledesigns.com/wiki/Wiki.jsp?
page=AppFuse