ColdFusioning.com
Pronunciation \kold-fy-zhn-ing\ Noun: The actions of one that writes ColdFusion.

Please check out my new site: http://ja.mesbrown.com/

ColdFusion / Railo Object Relational Mapping - ORM Source Code and Notes

Tonight I gave a presentation on using ORM with ColdFusion or Railo. You can watch the recorded presentation on Adobe Connect. As promised, I'm publishing the source code from the meeting.

Since the code uses ColdBox and a database, it requires some setup. If you follow the steps below, you'll be able to get setup to run the examples:

  • Setup ColdBox with a mapping ("/coldbox") to the ColdBox source
    (I'm using Version 3115)
  • Unzip the source files to a empty directory
  • Setup a new site (I'm using "http://office.scribble/" in the examples)
  • In ColdFusion Admin
    • Setup a mapping "scribble" that points at the directory
    • Create a datasource pointing at a new database
  • Update Application.cfc with the new datasource (line 24)
  • Call the application from the browser

At this point, you should see the ColdBox version number and a list of actions. You'll want to run the "Reload" link first so the tables are all created. I'm not sure why, but if you receive an error on the "Reload" page, refresh the browser a couple times and I'll complete properly.

If you have problems setting up ColdBox, please refer to the ColdBox Documentation at http://wiki.coldbox.org/

Most of the code you'll be utilizing is in either /handlers/dev/ or /model/objects/

Also, I added a deleteContact example that wasn't covered in the presentation. It should be pretty self explanatory.

Lastly, this code is a basis to build on and should not be used in a production environment.

If you have any questions or problems, please leave a comment below.

Download Source Code



Object Relational Mapping (ORM) in ColdFusion 9 and Railo Free Presentation

Tomorrow night I will be presenting on using ORM with ColdFusion and Railo. We'll be looking at the concepts behind ORM but will also be exploring code samples and practical, real-world utilization of ORM.

The presentation will be in Tampa, FL and will also be offered via Adobe Acrobat Connect. That's right, we'll be broadcasting live and accepting questions from remote attendees!

If you ever have wondered what ORM was and how you could use it or if you're already using ORM but would like to learn more and solidify your understanding, this is the presentation to attend.

View more information or register over on Meetup.

ColdFusion Now Supports Multiple Data Sources for ORM

With the ColdFusion 9.0.1 update, you can now use multiple data sources with ORM. When you create your objects, use the "datasource" attribute to specify which each should use. Below is an example of it in use:

view plain print about
1component        persistent        = true
2            accessors        = true
3            entityname        = 'Contact'
4            hint            = 'This object holds contacts and their associated properties'
5            datasource        = 'mydatasource' {
6
7property name='ContactID' type='numeric' persistent=true ormtype='integer' fieldtype='id' generator='increment';
8}

If you don't specify a data source, the default data source is used.

Introduction to ColdFusion ORM

After coming back from MAX and seeing how wonderful ORM is, I had to jump right into it. I'm using ORM for the Tour de ColdFusion so I will post some code samples as I write them. The first set is an introduction to ColdFusion ORM.

The first thing you have to do is turn ORM on. You do this simply by adding the two following lines to your Application.cfc:

view plain print about
1<cfset this.datasource = "yourDataSourceName" />
2<cfset this.ormEnabled = true />

The DataSource name must match the one you created in the ColdFusion Administrator that is correctly pointed at a database. The next thing we'll do is define our table. We're going to call this one Categories.cfc:

view plain print about
1<cfcomponent persistent="true">
2 <cfproperty name="CategoryID"
3         type="numeric"
4         validate="integer"
5         setter="false"
6         hint="I am the unique ID of the property."
7         persistent="true"
8         fieldtype="id"
9         column="categoryid"
10         ormtype="integer"
11         generator="identity"
12         length="10"
13    />

14 <cfproperty
15        name="CategoryName">

16 <cfproperty
17        name="ParentID">

18 <cfproperty
19        name="theOrder">

20 <cfproperty
21        name="Deleted">

22</cfcomponent>

This defines the Unique ID named as Category that auto-increments. We defined some other simple columns. Next we're going to insert some data into the database:

view plain print about
1<cfscript>
2    newObj = EntityNew("Categories");
3    newObj.setCategoryName("UI Controls");
4    newObj.setParentid("2");
5    EntitySave(newObj);
6    writedump(newObj.getCategoryID());
7    ormflush();
8
</cfscript>

All this does is set the category name, parentid and saves it. It also retrieves the new CategoryID from the database. The last thing we're going to do is get some data back out of the database:

view plain print about
1<cfscript>
2 categories = EntityLoad("categories", true);
3 writedump(categories);
4
</cfscript>

This is very similar to doing except ORM loads and array rather than a query:

view plain print about
1<cfquery name="getCategories">
2SELECT * FROM Categories
3</cfquery>
4<cfdump var="#getCategories#" />

Lastly, if you want to select a single record from the database you would use this code:

view plain print about
1<cfscript>
2 categories = EntityLoad("categories", 5, true);
3 writedump(categories);
4
</cfscript>

With "5" being the CategoryID of the record you want to pull from the database.

ORM is truly a game-changer in the web development industry. It is going to enable ColdFusion developers to create applications even faster than we could before. I would recommend not waiting to learn it because it is only going to save you time in the long run.

ColdFusion ORM: Error while executing the Hibernate query - table is not mapped exception

I'm starting to learn ORM and figured I'd post some simple but confusing problems I've run into. The first is I kept getting the error:

Error while executing the Hibernate query.
org.hibernate.hql.ast.QuerySyntaxException: tablename is not mapped
This was my ORM code:

view plain print about
1<cfset request.id = 0 />
2<cfscript>
3categories = ormExecuteQuery("from categories where ParentID = ?", [request.ID]);
4
</cfscript>

Apparently, because my cfc is named Categories.cfc and I didn't capitalize "categories" inside of the ormExecuteQuery, it was throwing the error. If I cahnge it to:
view plain print about
1<cfset request.id = 0 />
2<cfscript>
3categories = ormExecuteQuery("from Categories where ParentID = ?", [request.ID]);
4
</cfscript>

The code works. A simple capitalization error that doesn't seem obvious because ColdFusion is largely not case sensitive.

Recent Comments

RSS

Subscribe