Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
The Hibernate notion of a session is something between connection and transaction. It may be easier to think of a session as a cache or collection of loaded objects relating to a single unit of work. Hibernate
can detect changes to the objects in this unit of work. We sometimes call the Session a persistence manager because it's also the interface for persistence-related operations such as storing and retrieving objects. Note that a Hibernate
session has nothing to do with the Web-tier HttpSession. When we use the word session, we mean the Hibernate session. We sometimes use user session to refer to the HttpSession object.
SessionFactory interface
The application obtains Session instances from a SessionFactory. Compared to the Session interface, this object is much less exciting.
The SessionFactory is certainly not lightweight! It's intended to be shared among many application threads. There is typically a single SessionFactory for the whole application—created during application initialization, for example. However, if your application accesses multiple
databases using Hibernate, you'll need a SessionFactory for each database.
The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that
has been read in one unit of work and may be reused in a future unit of work (only if class and collection mappings specify
that this second-level cache is desirable).
Configuration interface
The Configuration object is used to configure and bootstrap Hibernate. The application uses a Configuration instance to specify the location of mapping documents and Hibernate-specific properties and then create the SessionFactory.
Even though the Configuration interface plays a relatively small part in the total scope of a Hibernate application, it's the first object you'll meet
when you begin using Hibernate.
Transaction interface
The Transaction interface is an optional API. Hibernate applications may choose not to use this interface, instead managing transactions
in their own infrastructure code. A Transaction abstracts application code from the underlying transaction implementation—which might be a JDBC transaction, a JTA UserTransaction, or even a Common Object Request Broker Architecture (CORBA) transaction—allowing the application to control transaction
boundaries via a consistent API. This helps to keep Hibernate applications portable between different kinds of execution environments
and containers.
Query and Criteria interfaces
The Query interface allows you to perform queries against the database and control how the query is executed. Queries are written in
HQL or in the native SQL dialect of your database. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the
query.
The Criteria interface is very similar; it allows you to create and execute object-oriented criteria queries.
To help make application code less verbose, Hibernate provides some shortcut methods on the Session interface that let you invoke a query in one line of code. We won't use these shortcuts; instead, we'll always use the Query interface.
A Query instance is lightweight and can't be used outside the Session that created it.
Callback interfaces allow the application to receive a notification when something interesting happens to an object—for example, when an object is loaded, saved, or deleted. Hibernate applications don't need to implement these callbacks, but they're useful for implementing certain kinds of generic functionality, such as creating audit records.
The Lifecycle and Validatable interfaces allow a persistent object to react to events relating to its own persistence lifecycle. The persistence lifecycle is encompassed by an object's CRUD operations. The Hibernate team was heavily influenced by other
ORM solutions that have similar callback interfaces. Later, they realized that having the persistent classes implement Hibernate-specific
interfaces probably isn't a good idea, because doing so pollutes our persistent classes with nonportable code. Since these
approaches are no longer favored, we don't discuss them.
The Interceptor interface was introduced to allow the application to process callbacks without forcing the persistent classes to implement
Hibernate-specific APIs. Implementations of the Interceptor interface are passed to the persistent instances as parameters.
A fundamental and very powerful element of the architecture is Hibernate's notion of a Type. A Hibernate Type object maps a Java type to a database column type (actually, the type may span multiple columns). All persistent properties
of persistent classes, including associations, have a corresponding Hibernate type. This design makes Hibernate extremely
flexible and extensible.
There is a rich range of built-in types, covering all Java primitives and many JDK classes, including types for java.util.Currency, java.util.Calendar, byte[], and java.io.Serializable.
Even better, Hibernate supports user-defined custom types. The interfaces UserType and CompositeUserType are provided to allow you to add your own types. You can use this feature to allow commonly used application classes such
as Address, Name, or MonetaryAmount to be handled conveniently and elegantly. Custom types are considered a central feature of Hibernate, and you're encouraged
to put them to new and creative uses!
Much of the functionality that Hibernate provides is configurable, allowing you to choose between certain built-in strategies. When the built-in strategies are insufficient, Hibernate will usually let you plug in your own custom implementation by implementing an interface. Extension points include:
IdentifierGenerator interface)
Dialect abstract class)
Cache and CacheProvider interfaces)
ConnectionProvider interface)
TransactionFactory, Transaction, and TransactionManagerLookup interfaces)
ClassPersister interface hierarchy)
PropertyAccessor interface)
ProxyFactory interface)
Hibernate ships with at least one implementation of each of the listed interfaces, so you don't usually need to start from scratch if you wish to extend the built-in functionality.
Archived Discussions (Read only)