cborm
Install
v3.x
v3.x
  • Introduction
  • Intro
    • Release History
      • What's New With 3.9.0
      • What's New With 3.8.0
      • What's New With 3.7.0
      • What's New With 3.6.0
      • What's New With 3.5.0
      • What's New With 3.4.0
      • What's New With 3.3.0
      • What's New With 3.2.x
      • What's New With 3.1.0
      • What's New With 3.0.0
    • About This Book
    • Author
  • Getting Started
    • Overview
    • Installation
    • Basic Crud - Services
    • Basic Crud - ActiveEntity
  • Base ORM Service
    • Overview
    • Service Properties
    • Concrete Services
    • Service Methods
      • Criteria Queries
        • getRestrictions
        • newCriteria
      • Creation - Population
        • new
        • populate
        • populateFromJSON
        • populateFromQuery
        • populateFromXML
      • Counters
        • count
        • countWhere
        • exists
      • Deleting Entities
        • delete
        • deleteAll
        • deleteByID
        • deleteByQuery
        • deleteWhere
      • Entity Convenience Methods
        • getDirtyPropertyNames
        • getEntityGivenName
        • getEntityMetadata
        • getKey
        • getKeyValue
        • getPropertyNames
        • getTableName
        • isDirty
        • refresh
      • Finders
        • findit
        • findOrFail
        • findByExample
        • findWhere
        • findAll
        • findAllWhere
      • Getters
        • get
        • getOrFail
        • getAll
      • ORM Session
        • clear
        • evict
        • evictCollection
        • evictQueries
        • getSessionStatistics
        • isSessionDirty
        • merge
        • sessionContains
      • Querying
        • executeQuery
        • list
      • Saving Entities
        • save
        • saveAll
      • Utility Methods
        • autoCast
        • createService
        • idCast
        • nullValue
        • when
    • Dynamic Finders- Counters
      • Method Signatures
      • Method Expressions
      • Query Options
    • Automatic Java Types
  • Virtual Services
    • Overview
    • Service Properties
    • Concrete Virtual Services
  • Active Record
    • Active Entity Overview
    • Constructor Properties
    • Usage
    • Validation
  • Criteria Queries
    • Criteria Builder
      • Getting Started
      • Restrictions
        • Value Casting
        • SQL Restrictions
      • Modifiers
      • Results
      • Associations
      • Projections & Aggregates
    • Detached Criteria Builder
      • Getting Started
      • Projections
      • Subqueries
      • DetachedSQLProjection()
      • Criterias
      • Associations
    • Help! I'm Not Getting the Result I expected!
  • Advanced Features
    • Automatic REST Crud
    • Hibernate Logging
    • Mementifier
    • ORM Events
      • Custom Event Handler
    • Unique Property Validation
Powered by GitBook
On this page
  • Enabling The Event Handler
  • Listening to Events

Was this helpful?

Edit on Git
Export as PDF
  1. Advanced Features

ORM Events

PreviousMementifierNextCustom Event Handler

Last updated 5 years ago

Was this helpful?

Hibernate can announce events to listener objects so they can tap in to the life-cycle of the entities. You can either listen to the events globally or within the entity itself by just declaring a few listener methods. The cborm module taps in to the global Hibernate events and re-transmits them as. This allows you to intercept ORM events via multiple CFC listeners instead of the rigid approach of a single listener CFC which the ColdFusion engines give you.

This is achieved by the cborm.models.EventHandler class and by telling the application about it. The event handler also (if configured) will talk to WireBox and auto wire entities with dependencies. Just add your dependency injection properties and off you go with entity injection.

Enabling The Event Handler

You can enable the cborm event handler by opening the Application.cfc and adding two settings:

  • eventHandling

  • eventHandler

Application.cfc
this.ormSettings = {
    cfclocation="model",
    dbcreate = "update",
    dialect = "MySQLwithInnoDB",
    logSQL = true,
    // Enable event handling
    eventhandling = true,
    // Set the event handler to use, which will be inside our application.
    eventhandler = "cbmorm.models.EventHandler"
};

Listening to Events

Below are the new interception points the ORM Event Handler exposes with the appropriate interception data it announces. Just create an Interceptor CFC, add the method with the name of the interception point and off you go!

Interception Point

Intercept Structure

Description

ORMPostNew

{entity}

Called via the postNew() event

ORMPreLoad

{entity}

Called via the preLoad() event

ORMPostLoad

{entity}

Called via the postLoad() event

ORMPostDelete

{entity}

Called via the postDelete() event

ORMPreDelete

{entity}

Called via the preDelete() event

ORMPreUpdate

{entity,oldData}

Called via the preUpdate() event

ORMPostUpdate

{entity}

Called via the postUpdate() event

ORMPreInsert

{entity}

Called via the preInsert() event

ORMPostInsert

{entity}

Called via the postInsert() event

ORMPreSave

{entity}

Called via the preSave() event

ORMPostSave

{entity}

Called via the postSave() event

ORMPreFlush

{entity}

Called before the Hibernate session is flushed. Triggered via the preFlush() event

ORMPostFlush

{entity}

Called after the Hibernate session is flushed. Triggered via the postFlush() event

With the exposure of these interception points to your ColdBox application, you can easily create decoupled executable chains of events that respond to ORM events. This really expands the ORM interceptor capabilities to a more decoupled way of listening to ORM events. You can even create different interceptors for different ORM entity classes that respond to the same events, extend the entities with AOP, change entities at runtime, and more; how cool is that.

component extends="coldbox.system.Interceptor"{

    function ORMPostLoad( event, interceptData, rc, prc ){
        // audit the data.
        var state = interceptData.entity.getMemento();
        autidService.logState( state, interceptData.entity );    
    }

}

That's it, now the cborm event handler will listen to the ORM events, re-broadcast them and you can create to listen to them.

ColdBox Interceptors
ColdBox interception points