Relational
databases are the core of most enterprise applications. However, when you map a
relational database to objects, it becomes a challenge. Object relational
mapping (ORM) is a programming framework that allows you to define a mapping
between application object model and the relational database.
In an
object model, the application objects are not aware of the database structure.
Objects have properties and references to other objects. Databases consist of
tables with columns that maybe related to other tables. ORM provides a bridge
between the relational database and the object model.
By using
ORM, you can access and update data entirely using the object model of an
application. ORM provides features such as:
§ Database vendor independence
§ Caching
§ Concurrency
§ Performance optimization
Before ORM
release in coldfusion 9, database access was achieved by:
§ Managing relational data using
tags such as cfquery, cfinsert, and cfupdate, which handle SQL statements.
§ Managing objects using
ColdFusion components (CFCs), and object lifecycle using the application itself
§ Writing SQL queries for each
CFC, even for basic CRUD (Create, Retrieve, Update, and Delete) operations.
The
complexity of managing these tasks increases as your application grows.
ColdFusion
ORM automates most of these tasks, which:
§ Makes application code cleaner
and more manageable
§ Enhances your productivity and
lets you develop database applications faster
§ Creates applications that can
run faster because of built-in ORM optimizations
§ Minimizes the amount of code
you write
Apart from
providing a framework for mapping the object model with the relational
database, ColdFusionORM provides data query and retrieval facilities.
Go through the following code files which will give idea about how
CRUD (Create, Retrieve,Update,Delete) actions can be performed using ORM -
Application.cfm
<cfcomponent displayname="Application" output="true">
<cfset This.name = "ORMApplication">
<cfset This.Sessionmanagement=true>
<cfset This.Sessiontimeout="#createtimespan(0,1,0,0)#">
<cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">
<cfset this.ormenabled = "true">
</cfcomponent>
Artists.cfm
<cfcomponent persistent="true">
<cfproperty name="id" column = "ARTISTID" generator="increment">
<cfproperty name="FIRSTNAME">
<cfproperty name="LASTNAME">
<cfproperty name="ADDRESS">
<cfproperty name="CITY">
<cfproperty name="STATE">
<cfproperty name="POSTALCODE">
<cfproperty name="EMAIL">
<cfproperty name="PHONE">
<cfproperty name="FAX">
<cfproperty name="thepassword">
</cfcomponent>
Index.cfm
<!--- <cfdbinfo
datasource="cfartgallery"
name="res"
type="columns"
table="ARTISTS">
<cfdump var="#res#">
--->
<!--- CRUD OPERATIONS - By Using ORM--->
<!--- 2. Retrieve --->
<cfset result = EntityLoad("ARTISTS")>
<!--- 1. Create --->
<cfscript>
try {
newArtistObj = EntityNew("artists");
newArtistObj.setfirstname("dattatray");
newArtistObj.setlastname("Smith");
newArtistObj.setaddress("5 Newport lane");
newArtistObj.setcity("San Francisco");
newArtistObj.setstate("CA");
newArtistObj.setPostalCode("90012");
newArtistObj.setphone("612-832-2343");
newArtistObj.setfax("612-832-2344");
newArtistObj.setemail("jsmith@company.com");
newArtistObj.setThePassword("jsmith");
EntitySave(newartistobj);
ormflush();
} catch(Exception ex) {
WriteOutput("<p>#ex.message#</p>");
}
</cfscript>
<!--- 3. Update --->
<cfset newArtistObj.setphone("612-832-1311")>
<cfset ormflush()>
<!--- 4. Delete --->
<cfset EntityDelete(newArtistObj)>
<cfset ormflush()>