Accessibility

Table of Contents

Introducing ORM in Adobe ColdFusion 9

What is an ORM?

While the term Object Relational Mapper sounds complex and scary, at a core level, the concept is actually fairly simple and straightforward. The crux of the matter comes from the fact that in applications, objects know about each other through references in memory, while in a relational database, tables of data refer to each other through primary and foreign key relationships. For example, a Musician object may know about an array of Instruments he can play, but in the database a Musician table will have a primary key, which is referred to by a foreign key to build a relationship between a Musician and his Instruments.

The job of an ORM, then, is to translate objects into relational data and relational data into objects, without the developer having to do any of the hard work. While many ORMs have more features above and beyond this core tenet, at a basic level it really is nothing more complicated than that.

Why use an ORM?

The explanation of an ORM sounds very good academically, but what benefits can you expect to receive as a developer? Essentially, the major reason to use an ORM is to save you time when developing applications. While obviously it will always take time to first master a new tool, I think this point is so important, that I will say it again: An ORM will save you a lot of time when developing applications.

This includes reduced implementation time up front when first developing your application, since an ORM will save you from the hours of tedious drudgery that is writing create, read, update, and delete SQL statements and the ColdFusion code to go with them. Not only that, but an ORM will reduce the number of potential error spots in your application, since you have a single configuration point for all your persistence needs. With an ORM, having a correct INSERT statement, but a spelling mistake in your UPDATE statement tends to be a thing of past.

Maintenance time is also reduced. For example, if you wanted to add an extra property to your object, normally you would have to make changes in a myriad of places, from UPDATE, SELECT, and INSERT statements, to ColdFusion code. All of these changes introduce the possibility of making further programming errors. When using an ORM, it is usually one change in a single place, and that is all you need to do.

What is Hibernate?

To quote the Hibernate website, "Hibernate is a powerful, high performance object/relational persistence and query service." It is also one of the most popular Java persistence solutions currently in use.

Hibernate provides all the create, read, update, and delete (CRUD) tools you need to push and pull objects in and out of a relational database via a sophisticated mapping configuration. It also has tools for enhanced querying, generating your database schema, caching, event interception, and much more.

In the long term, it is definitely worth learning more about how Hibernate works so that you can begin to understand what ColdFusion is actually doing behind the scenes and get yourself out of some tricky gotcha situations. For now, however, you can get started with the ORM integration without knowing all of the details, because much of complicated work has been done for you by the ColdFusion developer team.