Railo ORM initial setup

This is a reminder post that when playing around with ORM you don't have to use a pre-created database! In fact you can setup the database, datasource, tables, and even table relationships without opening an SQL Gui/manager or writing a single line of SQL. When developing a new site or like me playing around with ORM you don't want to be tied to the data layer in anyway and thanks to the in built relational databases that comewith our beloved CFML engines you don't.

This all started with me putting together some code and a post about ORM and scripting and rather than worry about the database layer I simply set up a HSQLDB (Hypersonic SQL DB) datasource in the Railo administrator ( with Adobe CF simply create an Apache Derby Embedded datasource in the Adobe ColdFusion 9 administrator).

To do this log in to your Railo administrator and go Datasource under Services and "Create new datasource" selecting HSQLDB (Hypersonic SQL DB) from the dropdown. On the next page give your Database a Name, Username, and Password then click create.

ColdFusion / Railo ORM datasource HSQLDB setup

You're now set to go. In my example I have called the datasource "ormTest", if you want to check that Railo has done its job then you can try the following snippet

Next you'll need to do is setup your ORM settings in Application.cfc

component output="false"{ // application variables = "OrmEx_#hash( getCurrentTemplatePath() )#"; this.sessionmanagement = true; // orm settings this.datasource = "ormTest"; this.ormEnabled = true; this.ormsettings = { dbcreate = "dropcreate", // This value drops the table if it exists and then creates it logSQL = true, // SQL queries are logged to the console cfclocation = "models" // Specifies the directory(or multiple off) to search for persistent CFCs to generate the mappings };}

With your Application.cfc defining your Datasource and ORM we need to add our model (in a folder called Model which was defined in the orm setting (this.ormsettings.cfclocation = "models"). The good news is that Hibernate will use our Models to go off and create the tables when the application is loaded thanks to the dropcreate setting above. Like I said we're not going to write a single line of SQL. Create a folder on the root called models and add users.cfc with the following code in it

component displayname="User" hint="I am a model of a user" output="false" persistent="true"{ property name="userid" generator="increment" index="true" fieldtype="id" ormtype="int"; property name="username" ormtype="string"; property name="password" ormtype="string" ; property name="roleid" ormtype="int" ;}

And you're done. You can modify the Application.cfc adding the following and see your table outputted in an array, though there won't be much there untillyou add some records

public boolean function onRequestStart(String targetPage){ writeDump( var = EntityLoad( 'users' ), abort = 1 ); return true;}

n.b. If you want to add a user quickly here's the code

m = new model.users();m.setUsername( 'andy' );m.setPassword( 'password' );m.setRoleid( 1 );entitySave( m );ormflush(); //Flushes all pending CRUD operations