Hibernate Session is a thread-safe and short-lived object that represents a conversation between the application and the persistence layer. This is required to perform CRUD operations (Create, Delete, Update, Retrieval) on a persistent entity in Hibernate. For ColdFusion applications that use ORM, this session is automatically managed by ColdFusion. Hibernate sessions also act as the first level of cache, which ensures that only one copy of an object exists in the session.
When CRUD operations are performed in the session, data of the entity is not synchronized with the database immediately. That is, the SQL statements for the operations are not issued immediately and they are queued. The data is synchronized with the database when the session is flushed. When the session is flushed, the SQL operations are performed in the following order:
all entity deletions, in the same order the corresponding objects were deleted using EntityDelete()
The only exception to this is that objects with nativeId generation are inserted immediately when the object is saved.
ColdFusion creates and manages Hibernate sessions only if ormenabled is set to true in application scope.
When the ColdFusion application starts, it builds the Hibernate session factory that is available for the application life time. This factory is used to create Hibernate sessions that manage the persistent object lifecycle. When the first CRUD method is called, a Hibernate session gets created and is closed when the request ends or when the
ormclosesessionmethodis called. For details on how ColdFusion works with Hibernate, see the Architecture.
In multiple data source scenarios supported in ColdFusion 9 Update 1, there are multiple sessions (one for each data source) in the same request. For all entity functions, the appropriate sessions are used transparently.
ColdFusion exposes a few methods to let CFML developers work with the Hibernate sessions directly. ORM session-related functions also take optional data source argument. If you do not specify a data source, the default data source specified for ORM is used. The methods are as follows: