ColdBox, ORM made easy

ColdBox has the ability to leverage of the ORM side of ColdFusion, and also has a few others like Transfer and reactor built into it. I am going to focus on using the ORM side of ColdFusion 9, and demonstrate how easy it is to use in ColdBox.

By now most of us would have heard about how ColdFusion 9 has integrated hibernate, to give us a very nice and flexible layer to our data objects. And I must say that even if you are using Transfer and Reactor, if you have been designing your application with a tier that separates the layers, one should be able to remove that tier and slot the new one in with less effort than you think.

I won't focus on that side of things, but I will give you a very basic understanding on getting up on your feet with little effort.

Creating Persistent Entities.

Entities as they are commonly know in ColdFusion are what are classed as Domain Objects, don't worry too much on the naming of it, you just need to know that they are persistent objects in ColdFusion. Persistent means that by definition that when created, loaded they will be persisted back to the database. For now I will not go into to much details on this as there are plenty of good articles out there, and explain it more than what this post is about.

ColdFusion 9 has given us some great enhancements, and this is by no means one of these. So lets look at a very simple persistent entity that we will use in our examples, and by no means a complete example either.

component persistent='true' {
property name='UserID' ormtype='integer' fieldtype='id' generator='native';
property name='Username' ormtype='string' length='25';
property name='Active' ormtype='boolean';
property name='Password' length='20';
}

This is a very simple user object that can be persisted, also created updated to the database, depending on how you development practice defines this. But lets not worry about that for now, instead we are going to assume that this is going to update/create the tables for us.

Using ORM in a Handler

In a previous post I spoke about DI or Dependency Injection, that we can use to help us minimise what code we do end up writing. When using ORM in ColdBox this is will be no different, and is very simply a good way to have the framework give us what we need without manually writing code for it.

And to use this in a handler one only needs to do this at the top of your handler.

property name="ORMService" inject='coldbox:plugin:ORMService';

So lets say in our handler we want to get a user, then all we need to do is write this in our action.

local.criteria = { username = event.getValue('username',''), password = arguments.event.getValue('password','')};
rc.user = ORMService.findWhere(entityName='user', criteria = local.criteria);

This code is very simple and easy to understand. The first line we are creating a structure with the keys username and password, and getting the values passed in from a form. The second line we are then calling the ColdBox ORMService to find everything on the entity user, that will match the criteria that we pass it, which in this case is the username and password. In which we will get a result of the entity returned to us.

As you can see it is fairly simple, and not really hard to understand.

For further reading head on over to the ColdBox website and read on the other methods that the service has available that can help make your job a lot easier.


ColdBox, ORM made easy - http://cfbloggers.org/?c=44145 (via @cfbloggers) Aug 31, 2010
ColdBox, ORM made easy - http://cfbloggers.org/?c=44145 Aug 31, 2010

  • Sebastiaan's Gravatar A question from someone that has only "seen" Hibernate onceĀ (never worked with it) and only knows ColdBox from visiting the website:

    How "safe" is using this data layer compared to using cfqueryparam's etc. Is that kind of security built into the ORM-engine?

    It may be comparing apples to oranges, but I'm just curious!
    # Posted By Sebastiaan | 9/2/10 2:48 AM
  • Andrew Scott's Gravatar That is actually a very good question, and if you use the BIND parameters you can be sure that it as safe as sqlquerparam. However when using HQL it can be very easy to not use bind paramters, rather than go into full descriptive here.

    Have a read of this excellent article on the matter

    http://www.12robots.com/index.cfm/2009/11/19/ORM-H...
    # Posted By Andrew Scott | 9/2/10 3:48 AM