ORM session management

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:

  1. all entity insertions, in the same order the corresponding objects were saved using EntitySave()

  2. all entity updates

  3. all collection deletions

  4. all collection element deletions, updates, and insertions

  5. all collection insertions

  6. 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.

Note: 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 ormclosesessionmethod is called. For details on how ColdFusion works with Hibernate, see the Architecture.

ColdFusion exposes a few methods to let CFML developers work with the Hibernate sessions directly. The methods are as follows: