All pages
Powered by GitBook
1 of 2

Loading...

Loading...

ORM Events

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 ColdBox interception points. 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

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.

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!

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.

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

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

ColdBox Interceptors

{entity}

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"
};
component extends="coldbox.system.Interceptor"{

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

}

Custom Event Handler

The ColdFusion documentation says that in order to create a global event handler that it must implement the CFIDE.orm.IEventHandler interface. So all you need to do is create the CFC and make it extend the core class that will provide you with all these capabilities: cborm.models.EventHandler

component extends="cborm.models.EventHandler"{
}

That's it! Just by doing this the CF ORM will call your CFC's event methods in this CFC and satisfy the interface. Of course you can override the methods, but always remember to fire off the parent class methods in order for the ColdBox interceptions to still work. You can then override each event method as needed. Below is a chart of methods you can override:

Listener Method

Description

postNew(entity)

This method is called by the ColdBox Base ORM Service Layers after a new entity has been created via its new() method.

The base event handler CFC you inherit from has all the ColdBox interaction capabilities you will ever need. You can find out all its methods by referring to the API and looking at that class or by inspecting the ColdBox Proxy class, which is used for enabling ColdBox interactions: coldbox.system.remote.ColdboxProxy

preLoad(entity)

This method is called before the load operation or before the data is loaded from the database.

postLoad(entity)

This method is called after the load operation is complete.

preInsert(entity)

This method is called just before the object is inserted.

postInsert(entity)

This method is called after the insert operation is complete.

preUpdate(struct oldData,entity)

This method is called just before the object is updated. A struct of old data is passed to this method to know the original state of the entity being updated.

postUpdate(entity)

This method is called after the update operation is complete.

preDelete(entity)

This method is called before the object is deleted.

postDelete(entity)

This method is called after the delete operation is complete.

preSave(entity)

This method is called before the save operation.

postSave(entity)

This method is called after the save operation is complete.

preFlush(entity)

This method is called before the Hibernate session is flushed.

postFlush(entity)

This method is called after the Hibernate session is flushed.