SessionFactory
thread-safe
immutable cache of compiled mapping
for a single database
multiple factories can be created for multiple database
uses ConnectionProvider
optionally maintains a second level cache of data that is reusable between transactions
Session
single-threaded
short-lived, represent a conversion
wraps a JDBC connection
factory for org.hibernate.Transaction (can span several transaction)
maintains first level cache of persistent objects and collections
Persistent objects and collections
short-lived single threaded
contains persistent state and business function
associated with exactly one Session
once session is closed, it's detached and free to use
Transient and detached objects and collections
either created and not persisted
or obtained from a closed session
Transaction (org.hibernate.Transaction)
single threaded, short-lived
under-laying is JDBC, JTA or CORBA transaction
ConnectionProvider
factory for, and pool of JDBC connection
not exposed to application, can be extended
TransactionFactory
not exposed, can be extended
ExtensionInterfaces
SessionFactory.getCurrentSession()
Configured by hibernate.current_session_context_class
configure full class name, or
three short names : "jta", "thread", "managed"
JTA transactions
initially assuming using
defines the scope and context of current session
can use JTA interface to demarcate transactions
recommended for most applications, whether or not deployed in JEE container
if used in JEE container supporting CMT (container managed transaction), can use annotation
Thread Local
(previously not recommended for production use)
Managed Session
developer is responsible for bind and unbind
Use Hibernate Transaction API
recommended if use plain Java SE without JTA