Home >
Example of Flex 3 and ColdFusion 9 ORM
One of the more cooler new features in ColdFusion 9 is the addition of the open source Hibernate framework. This provides ORM (Object Relational Mapping) support to ColdFusion, greatly adding a good dose of RAD to an already RAD platform. For Flex developers looking to hook their code to a back end server, ORM could be a great way to simplify and quickly prototype the services your front end require. I'm going to spend the next few blog entries demonstrating some examples of this. To be clear, this is not meant to teach you everything about ColdFusion or ORM. For that I strongly suggest taking a look at the excellent (and free) documentation provided by Adobe. My main goal here is to demonstrate how simple it can and give you enough information to go further. (And as always, I'd appreciate feedback on what you would like to see and what would be most helpful.)
Before going any further, I will assume that you have both ColdFusion 9 installed as well as Flex Builder 3. I'll be writing all my Flex code in version 3 as I'm still trying to find time to learn Flex 4. I'll also be making use of the cfartgallery database. This is installed when you select the "demos and documentation" options during your installation.
I'll begin by demonstrating the simple Flex front end I wrote. It has a grand total of one component, a grid, that I'll use to dump the data we will get from the ORM-enabled code on the server.
As you can see, there really isn't a lot going on here. I've got my grid (artGrid). I've got my RemoteObject, that points to my ColdFusion server and a CFC found at ormtest.model.artservice. (This translates to a file path of /webroot/ormtest/model/artservice.cfc.) When the Flex application is loaded, I immediately run the method getArt. This is passed to the ColdFusion server. When a good result occurs (hopefully!) I simply set the result immediately to my grid. I'm still somewhat of a Flex noob so forgive any obvious bad practices I've used here.
Ok, so what about the server side? If you've never used any type of ORM framework before, let me give a mile high overview of what it can provide. ORM frameworks, like Hibernate, provide an abstraction layer between your code and your database. They allow you to work with your data like objects instead of rows from a query. So given a table of people, for example, you can define a Person entity (in ColdFusion this is done with a CFC) that Hibernate can then map to the database table. This is a grossly inadequate description, but it gives you the basics.
In ColdFusion we enable ORM for an application by using a special file, the Application.cfc template. This template is used for multiple things, but for now, just know that I'm enabling ORM for my application and telling the server what my datasource is.
Next I'm going to create an entity, or ColdFusion Component, that will map to a table in my database. Even if you don't know ColdFusion, I'm sure you can figure out that each cfproperty tag is going to map a property of my entity to a column in my database table.
The last part of the puzzle is the actual service my Flex application is hitting. Let's take a look at that file:
Yeah, not a lot there, right? It's mainly short because my application does only one thing - list data. But notice how I didn't write one line of SQL here. Why? Again, Hibernate knows, based on my entity (the previous listing) how to map my object to the database table. Therefore there is no need for me to write boring, boilerplate SQL just get my data. I'm using the entityLoad function to simply ask for all of my art data. And that's it. Pretty simple, right? But does it work?
Woot. I won't win any design awards, but as you can see, it just... worked. I didn't have to do any handling of the data at all on the Flex side. I didn't have to write a lick of SQL on the server side. And all in all this is probably no more than 50 lines of code.
Of course, it is also trivial to the point of being useless too. In the next entry I'm going to make improvements on both the front end and the back end. You may notice that one of my columns, "mediaid", appears to be a foreign key. How difficult would it be to get the proper data for that FK to show up in the grid? I'll demonstrate that next!
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
<mx:RemoteObject id="artService" destination="ColdFusion" source="ormtest.model.artservice">
<mx:method name="getArt" result="getArtResult(event)" fault="generalFault(event)" />
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function init():void {
artService.getArt()
}
private function getArtResult(resultEvt:ResultEvent):void {
artGrid.dataProvider = resultEvt.result
}
private function generalFault(faultEvt:FaultEvent):void {
mx.controls.Alert.show('Fault event '+faultEvt.toString())
}
]]>
</mx:Script>
<mx:DataGrid id="artGrid">
</mx:DataGrid>
</mx:Application>
component {
this.name = "ormtest1";
this.datasource = "cfartgallery";
this.ormEnabled = true;
}
component persistent="true" {
property name="id" fieldtype="id" column="artid" ormtype="integer" generator="native";
property name="name" column="artname" ormtype="string";
property name="description" ormtype="string";
property name="price" ormtype="float";
property name="largeimage" ormtype="string";
property name="mediaid" ormtype="integer";
property name="issold" ormtype="integer";
}
component {
remote array function getArt() {
return entityLoad("art");
}
}





Facebook Application Development
Cool man! I have been wanting to try and learn Flex and this offers great insight about how to link CF and Flex...something that I would love to learn to do. Can't wait for your next post :)
Thnaks for the primer Raymond, will you demonstrate later on as well how to modify an object in Flex, send it back to ColdFusion and have the ORM update the appropriate database records?
thanks,
Roland
Nice one, flex 3 has confuzed and made my mobile ability fall through the floor, productivity dropped until I found this answer to my probs.
Thank you very much, I am closely following your site really give much useful information!..
indir