Coldfusion 9 ORM with Hibernate Part 1

There are a lot of blog posts already out there in the Coldfusion "blogosphere" here is my addition to the already great resources. I've decided to start this series of blog posts from the very first steps and go from there.

What is ORM?

ORM stands for Object-relational Mapping and basically is a method of creating a relationship between objects and a relational database. The "main" benefit of using ORM in is that it can substantially reduce the amount of code that a programmer needs to write. The mundane functions to Create, Read, Update and Delete (CRUD) elements in database tables is automatically handled by ORM as well as allowing the programmer to specify how objects relate to each other (one-to-many, one-to-one etc) that are then "translated" into sql queries when handling the objects.

Hibernate

Hibernate is most probably the most well known ORM library for the Java language and is what Coldfusion 9 uses for it's ORM framework. There are already a few ORM frameworks created specifically for the CFML language (Transfer, Reactor, etc) which have all be built using Coldfusion. Adobe has integrated Hibernate support into the Coldfusion engine.

Setting up your Application for Persistence

When you are creating an Application and you want to use Hibernate to persist your components (objects) you need to set up persistence in your Application.cfc component. A very simply Application.cfc would looks something like:


<cfcomponent output="false">
<cfset this.name="testingORM">
<cfset this.ormenabled=true>
<cfset this.ormsettings={datasource='test',dbcreate='dropcreate'}>
</cfcomponent>
I enable ORM using the this.ormenabled setting, and then added some additional orm settings on the next line. I specified the DataSource that I want to use for my application, and indicated that I want Hibernate to create the tables if they don't exist or drop and create new ones if they exist. Naturally in a production environment you would set dbcreate to update the database tables when/if they should change.

A Persistent Component

Keeping things very simple we will create a person component. In this first part the person will be very simple and we will make things a little more complicated later on.


<cfcomponent displayname="person" hint="I am a person object" output="false" persistent="true">
<cfproperty name="id" generator="native" fieldtype="id" >
<cfproperty name="firstname">
<cfproperty name="lastname">
<cfproperty name="dob" ormtype="date">
</cfcomponent>
In the cfcomponent tag we added persistent=true in order to tell CF that we want Hibernate to manage our object. The first property of our component is the ID of the person, we specify its fieldtype as id and specify that we want Hibernate to use the method to generate the identifier that is native to the underlying database (see the CF docs for all the different generators). The next interesting bit is the ormtype attribute which allows us to specify the datatype of the database field. There are a number of additional attributes but in this example they don't apply since I am having Hibernate create my database tables for me.

We now have a persistent component. Coldfusion automatically creates the setters and getters for our properties, additionally we can use a series of functions to perform the standard CRUD functions.

Using ORM on our Component

To Create and persist (update) the data of our component we use two functions:

  • entityNew(): creates a new entity
  • entitySave(): updates or inserts the data into the database for the specified entity.

To Read a person from the database we use the method entityLoad(). The entityLoad method can take a few different methods, in order to return ALL persons in our database we simply use entityLoad('person') to return an array of objects. To return a single person we use entityLoad('person',1,true) to return the person with the ID of 1, the true indicates to return the actual entity instead of an array of entities.

To Update an entity we use the entityUpdate() method.

To Delete an entity we first have to load the entity into a variable e.g.:


<cfset personToDelete= entityLoad('person',1,true)>
<cfset entityDelete(personToDelete)>

There we have the standard CRUD functions without having to write a single line of code, or a single query. We simply specified the correct properties and attributes in our Application.cfc and our component and we automatically have the functionality that we need.

Oh, one last rather useful function is the EntityToQuery() function, once you have called your entity load function you end up with an array of objects, queries are often easier to deal with so you can convert your array of objects into a query. For my person object I would do the following:


<cfset people = entityLoad('person')>
<cfset qryPeople = entityToQuery(people)>
<cfdump var="#qryPeople#">

Conclusion

We got a brief overview of what ORM is and what Hibernate is, we also created a very simple object that will be persisted in a database with the datasource name of "test". Notice we never bothered mentioning what DB it was we don't care either. Next we will expand our person object by adding a one-to-many relationship and seeing how we manipulate the related objects.

Happy Coding...

2 Comments to "Coldfusion 9 ORM with Hibernate Part 1"- Add Yours
Claude's Gravatar Great post Gary, congrats.
# Posted By Claude | 8/30/09 9:19 PM
Justin's Gravatar Nice info here. I'm looking forward to the other parts.
# Posted By Justin | 9/28/09 12:21 AM

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript jquery max2007 max2008 misc open source programming railo software technology ui

Recent Entries

No recent entries.

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List