5. Basic
Fundamentals
Conclusion
Problem
Hibernate - Definitions
by Hibernate.org
Hibernate is concerned with helping your
application to achieve persistence. . . Persistence
simply means that we would like our application’s
data to outlive the applications process. . . would
like the state of (some of) our objects to live
beyond the scope of the JVM so that the same
state is available later.
by Wikipedia.org
Hibernate is an object-relational mapping (ORM)
library for the Java language, . . . a framework for
mapping an object-oriented domain model to a
traditional relational database . . .
11. Basic
Fundamentals
Conclusion
Concepts
Plain Old Java Object (POJOs)
In general, a POJO is a Java object not bound by any restriction
other than those forced by the Java Language Specification.
However, due to technical difficulties and other reasons, in the
context of Hibernate, a POJO is defined as follow:
No-argument class constructor
Property accessor (get/set) methods
Class is not declared final nor has final methods.
Collection-typed attributes must be declared as interface
types.
14. Basic
Fundamentals
Conclusion
Data Model
Identity Mapping
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
Mapped classes must declare the primary key column of
the database table.
Generators using the native class will use identity or
sequence columns depending on available database
support. If neither method is supported, the native
generator falls back to a high/low generator method to
create unique primary key values.
The native generator returns a short, integer, or long value.
Hibernate documentation about Identity mapping here.
15. Basic
Fundamentals
Conclusion
Data Model
Property Mapping
<property name="startDate" column="start_date"
type="date"/>
A typical Hibernate Property mapping defines a POJO
property name, a database column name, and the name of
a Hibernate type, and it is often possible to omit the type.
Hibernate uses reflection to determine the Java type of the
property.
Details about Hibernate Types mapping here.
Hibernate documentation about Property mapping here.
16. Basic
Fundamentals
Conclusion
Data Model
Entity Mapping
<class name="Event" table="events">
<!-- define identity, properties, components,
collections, associations here... -->
</class>
A typical Hibernate Entity mapping defines a POJO class
name, a database table name.
By default, all class names are automatically “imported”
into the namespace of HQL
Hibernate documentation about Entity mapping here.
17. Basic
Fundamentals
Conclusion
Data Model
Component Mapping
<component name="componentName"
class="componentClass">
<!-- defines properties of the component here
these properties will be mapped to columns of
the enclosing entity-->
</class>
A Hibernate Component mapping is defined within an
Entity mapping several objects into one single table of the
enclosing entity.
Hibernate documentation about Component mapping here.
21. Basic
Fundamentals
Conclusion
Data Model
Collection Mapping
Common Collections: sets, lists, bags, maps of value types.
Value Type:
An object of value type has no database identity; it belongs
to an entity instance, and its persistent state is embedded
in the table row of the owning entity
Value-typed classes do not have identifiers or identifier
properties
27. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Object States
Transient
The object is not associated with any persistence
context. It has no persistent identity (primary key
value).
Persistent
The object is currently associated with a
persistence context. It has a persistent identity
(primary key value) and, perhaps, a corresponding
row in the database. Hibernate guarantees that
persistent identity is equivalent to Java identity.
28. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Object States (cont.)
Detached
The instance was once associated with a
persistence context, but that context was closed,
or the instance was serialized to another process.
It has a persistent identity and, perhaps, a
corrsponding row in the database. For detached
instances, Hibernate makes no guarantees about
the relationship between persistent identity and
Java identity
30. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Case 1: Making an object persistent
Item item = new Item();
item.setName("Playstation3 incl. all accessories");
item.setEndDate( ... );
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Serializable itemId = session.save(item);
tx.commit();
session.close();
31. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Case 2: Retrieving a persistent object
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.load(Item.class,
new Long(1234));
// Item item = (Item) session.get(Item.class,
// new Long(1234));
tx.commit();
session.close();
32. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Case 3: Modifying a persistent object
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.get(Item.class,
new Long(1234));
item.setDescription("This Playstation is
as good as new!");
tx.commit();
session.close();
33. Basic
Fundamentals
Conclusion
Persistent Lifecycle
Case 4: Making a persistent object transient
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.load(Item.class,
new Long(1234));
session.delete(item);
tx.commit();
session.close();
38. Basic
Fundamentals
Conclusion
HQL
Criteria API
Some developers prefer to build queries dynamically, using an
object-oriented API, rather than building query strings.
Hibernate provides an intuitive org.hibernate.Criteria
represents a query against a particular persistent class:
41. Basic
Fundamentals
Architecture
Hibernate Flexibility and Extendibility
Extension points:
Dialects (for different databases)
Custom mapping types
ID generators
Cache, CacheProvider
Transaction, TransactionFactory
PropertyAccessor
ProxyFactory
ConnectionProvider
Conclusion
42. Basic
Fundamentals
Why Hibernate?
Free, open source Java package
Release developers from data persistent related tasks,
help to focus on objects and features of application
No need for JDBC API for Result handling
Database almost-independence
Efficient queries
Conclusion