My First ColdBox ORM Setup

As promised I will be posting on my own experiences in setting up CF9's inbuilt ORM with ColdBox. Most of this will be for my own reference.

For this example I am using the M6 release of ColdBox so some of this may change by the final release.

Right, let's start.

First like a normal CF9 ORM project you have to tell your application you are using ORM. ColdBox has it's own set of tools which help integrate hibernate. The first is a must if you're going to use hibernate within CB, and that's the event handler! I would suggest reading up on the ORM event handler as it does a lot more and shows you how to setup Autowire to inject objects into your entities etc.

view plain print about
1// ORM Setup
2    this.ormEnabled = true;
3    this.datasource = "myDS";
4    this.ormSettings = {
5        dbcreate = "update",
6        dialect = "MicrosoftSQLServer",//Specifies the dialect.
7        logSQL = true,
8        eventhandling = true,
9        eventhandler = "model.ORMEventHandler",
10        flushAtRequestEnd = false
11    };

As you can see you have added eventhandling = true, and eventhandler = "model.ORMEventHandler" to the code above. You will now need to add the ORMEventHandler.cfc to your model folder (providing you are following the same folder structure of course).

Next I am going to setup my first Domain Object. Depending on how your using it you will need to create this in the same model folder.

For this example we will setup a simple object and I will assume you already have some understanding of this.

view plain print about
1component output="false" persistent="true" {
2
3 property name="user_id" column="user_id" type="numeric" ormtype="int" fieldtype="id" generator="increment";
4     property name="user_name" column="user_name" type="string" length="120" ormtype="string";    
5
6 users function init() output=false{
7 return this;
8 }
9
10}

Something is missing? A service layer, maybe? How are we going to now use this object within our handlers? Well again CB gives us more options here. First you can use CB Virtual Service Layer bounded to you domain object directly in your handler. From my understanding this creates a 1-1 relationship between the domain object and service. I don't like this implementation as you can only have one service layer per object.

Better is to extend the BaseORMService.cfc directly within your own service layer. You still get all the normal goodies, in fact it's the same but you will need to to pass in the entity name. So to inject my own service layer userService.cfc I would use...

view plain print about
1/**
2* User Handler
3*/

4component extends="coldbox.system.EventHandler" output="false"{
5    //Autowire DSL
6    property name="userService" inject="model:userService";
7
8}

How cool is that? I love CB. My service layer needs to just extent the base ORM so we now have access all the ORM goodies and have the options of overriding or creating or own functions within the custom service layer.

view plain print about
1/**
2* A service layer that handles all user operations
3*/

4component extends="coldbox.system.orm.hibernate.BaseORMService"{
5
6    public userService function init(){
7     super.init(useQueryCaching=true);
8     return this;    
9    }
10}

Want to see an example of how we do that? So without writing any coding inside the custom service I can just use or base ORM service like this

view plain print about
1/**
2* User Handler
3*/

4component extends="coldbox.system.EventHandler" output="false"{
5    //Autowire DSL
6    property name="userService" inject="model:userService";
7    function login(event){
8        var rc = event.getCollection();
9        userService.get("user",rc.user_id);
10        event.setView("user/login");
11    }    
12}

Or I could write my own like this

view plain print about
1/**
2* User Handler
3*/

4component extends="coldbox.system.EventHandler" output="false"{
5    //Autowire DSL
6    property name="userService" inject="model:userService";
7    function login(event){
8        var rc = event.getCollection();
9        userService.getmyUsers("user",rc.user_id);
10        event.setView("user/login");
11    }    
12}

I think that's darn cool! I will post more over the next few weeks as I play more :)

Aug31

Comments 0