ColdFusion 9 ORM

collectivemodernΛογισμικό & κατασκευή λογ/κού

30 Ιουν 2012 (πριν από 5 χρόνια και 6 μήνες)

479 εμφανίσεις

White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.1
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.1
ColdFusion 9
ORM
Sven Ramuschkat
Herrlich & Ramuschkat GmbH
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
2
2
Agenda

What is ORM?

Why do you want it?

How does ColdFusion 9 provide it?

Advanced Features of ORM
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.3
What is ORM?
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
4
4
ORM

Object-relational mapping

A PersistenceLayer connecting objects in an object-oriented system to
data stored in a relational database.

Object-relational mapping (aka ORM, O/RM, and O/R mapping) is a
programming technique for converting data between incompatible type
systems in relational databases and object-oriented programming
languages.

This creates, in effect, a "virtual object database" that can be used from
within the programming language.

Proper object-relational integration requires a strategy for mapping the
object model to the relational model in order for Java objects to become
persistent to the RDBMS. A persistent object is one that can
automatically store and retrieve itself in permanent storage.
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.5
ORM
means
“Write Less SQL”
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.6
ORM
means
“Write Less Code”
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
7
7
ORM

ORM is a technology that allows you to map objects in your applications
to database tables without writing SQL.

In ColdFusion this means:

ORM maps CFC’s to tables in your database

You define these maps using <cfproperty>
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.8
Why do you want
it?
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
9
9
Inline Queries
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
10
10
Inline Queries

Pros

Extremely Easy to write

Cons

Encourages cut and paste coding

Hard to maintain

Doesn’t allow for reuse
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
11
11
DAO Way
DAO
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
12
12
DAO way

Pros

Easy to write

Easy to keep track of things

Don’t really need that much management

Cons

Lots of code to handle CRUD

No encapsulation of common formatting and display options

Create vs Update decisions
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
13
13
DAO + Service + Bean Way
DAO
Service
Bean
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
14
14
DAO + Service + Bean Way

Pros

Encapsulation

Less decisions at the UI level

Cons

Still lots of code to handle CRUD

Now even more code to handle service and bean

So much code you start to require management
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
15
15
ORM Way
Persisted CFC
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
16
16
ORM Way

Pros

Easy to write

Easy to keep track of things

Don’t really need that much management

Much much less code

Cons

More free time?
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.17
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.17
Question
Which method is the
most terse?
(Least lines of code)
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
18
18
How Many Lines of Code?
0
50
100
150
200
250
300
Inline SQL
DAO
Method
Service +
Bean +
DAO
ORM
64
128
295
62
Lines of Code
Lines of Code
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.19
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.19
ColdFusion’s ORM
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
20
20
Hibernate

Java Based

Mature

Open Source
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.21
Objects
Or how you can build
objects from tables
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
22
22
CFC Mapping

Allows mapping CFC’s to tables
<cfcomponent persistent="true">
<cfproperty name="personID" fieldtype="id" generator="native” />
<cfproperty name="firstName" datatype="string" />
<cfproperty name="lastName" datatype="string" />
<cfproperty name="email" datatype="string" />
<cfproperty name="hiredate" datatype="date" />
<cfproperty name="createdOn" datatype="date" />
</cfcomponent>
• personID int
• firstName varchar(128)
• lastName varchar(128)
• email varchar(128)
• hiredate datetime
• createdOn datetime
Person
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
23
23
Relationships

One-To-One

One-To-Many

Many-To-One

Many-To-Many
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
24
24
One-To-Many
<cfcomponent persistent="true">
<cfproperty name="departmentID" fieldtype="id" generator="native" />
<cfproperty name="name" datatype="string" />
<cfproperty name="employees" fieldtype="one-to-many"
cfc="employee" fkcolumn="departmentID"
cascade="all-delete-orphan"
singularName="employee" />
</cfcomponent>
• departmentID int
• name varchar(128)
Department

employeeID int
• name varchar(128)
• departmentID int
Employee
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
25
25
Many-to-One
<cfcomponent persistent="true” >
<cfproperty name=”name" column=”name" />
<cfproperty name="department" fieldtype="many-to-one" cfc="department" />
</cfcomponent>
• departmentID int
• name varchar(128)
Department

employeeID int
• name varchar(128)
• departmentID int
Employee
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
26
Join Mapping
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
27
Join Mapping
• productId int
• name varchar(128)
• cost decimal(10,2)
• createdOn datetime
Product
• productId int
• blurb text
ProductMarketing
<cfcomponent persistent="true" table="product">
<cfproperty name="productID" fieldtype="id" generator="native" />
<cfproperty name="title">
<cfproperty name="description">
<cfproperty name="blurb" table="productMarketing" joinColumn="productID" />
</cfcomponent>
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.28
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.28
Support Features
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
29
Transactions
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
30
30
Caching

Session Level Cache

Secondary Cache
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
31
31
Event Handlers

preInsert

postInsert

preLoad

postLoad

preUpdate

postUpdate

preDelete

postDelete

Application Wide

Per Entity
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
32
32
HQL

Straight from the docs:

In general, use HQL in the following scenarios:

The query is not specific to a particular object but only to some fields in the object.

To retrieve some fields of the object without loading the object.

When you use table joins.

When you use aggregate functions like min, max, avg, and count.

To retrieve entities by specifying a filter that needs to use operators other than AND.
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
33
33
Databases Supported

DB2

DB2AS400

DB2OS390

Derby

PostgreSQL

MySQL

MySQLwithInnoDB

MySQLwithMyISAM

Oracle

Oracle8i

Oracle9i

Oracle10g

Sybase

SybaseAnywhere

Microsoft SQL Server

Informix
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.34
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.34
Important Concepts
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
35
35
CFQuery is a live and well.

Multiple datasource per application

Reporting

Preference
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
36
36
It Ain’t Over till it’s over

Hibernate pulls data into objects

Objects go into session level cache

Changes occur in the cache

Changes don’t go to the database until Ormis Flushed

This has several implications

Transactions

Manual Flushes
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
37
37
Why so many options?

Objects and tables don’t line up
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
38
38
RAD Workflow

ColdFusion Builder to generate ORM code

Modify ORM to meet your needs

Display functions

Business Logic

Write your front end

ColdFusion created HTML

ColdFusion UI components

Flex front End
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
39
39
Demo1: Working with ORM Data

Instead of accessing the properties directly, use get/set methods

ColdFusion automatically generates getters/setters based on the <cfproperty> tags

Override the automatic methods and create custom ones

Loop over array to access data or convert arrays to a ColdFusion query
using the entityToQuery() function
<!---- query data ---->
<cfset data = entityLoad('Artist') />
<!---- display results ---->
<cfoutput>
<ul>
<cfloop from="1"to="#arrayLen(data)#"index="i">
<cfset artist = data[i] />
<li>#artist.getFirstName()# #artist.getLastName()#</li>
</cfloop>
</ul>
</cfoutput>
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
40
40
Demo2: Creating Custom Methods to Access Properties
<cfcomponent persistent="true" table="Artists" entityname="Artist">
<cfproperty name="ArtistID" fieldtype="id" />
<cfproperty name="FirstName" />
<cfproperty name="LastName" />
<cfproperty name="Email" />
<cfproperty name="Address" />
<cfproperty name="City" />
<cfproperty name="State" />
<cfproperty name="PostalCode" />
<!---- getFullName() combines FirstName & LastName ---->
<cffunction name="getFullName" returntype="string">
<cfset var fullname = getFirstName() & ' ' &
getLastName() />
<cfreturn fullname/>
</cffunction>
</cfcomponent>
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
41
41
Demo3: Saving Data with ORM
<!--- query a single record --->
<cfset artist = entityLoad('Artist', 1, true) />
<!--- update properties --->
<cfset artist.setFirstName('Adam') />
<cfset artist.setLastName('Lehman') />
<cfset artist.setEmail('adrock@adobe.com') />
<cfset artist.setAddress('123 Main Street') />
<cfset artist.setCity('RockVegas') />
<cfset artist.setState('MD') />
<cfset artist.setPostalCode('20852') />
<!--- save entity to database--->
<cfset entitySave(artist) />
<!--- dump results --->
<cfdump var="#artist#" />
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
42
42
Demo4: Working with Relationships
<cfcomponent persistent="true" table="Artists" entityname="Artist">
<cfproperty name="ArtistID" fieldtype="id" generator="identity" />
<cfproperty name="FirstName" />
<cfproperty name="LastName" />
<cfproperty name="Email" />
<cfproperty name="Address" />
<cfproperty name="City" />
<cfproperty name="State" />
<cfproperty name="PostalCode" />
<cfproperty name="Art" fieldtype="one-to-many" cfc="Art"
fkcolumn="ArtistID" />
<!--- getFullName() combines FirstName & LastName --->
<cffunction name="getFullName" returntype="string">
<cfset var fullname = getFirstName() & ' ' & getLastName() />
<cfreturn fullname/>
</cffunction>
</cfcomponent>
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
43
43
Demo4: Advanced ORM Queries
<!---- query records by example ---->
<cfset data = entityLoadByExample(artist) />
<!--- query all records by using HSQL --->
<cfset data = ormExecuteQuery("from Artist") />
<!--- define search parameters --->
<cfset params = {state='CA'} />
<!--- query matching records --->
<cfset data = ormExecuteQuery("from Artist where State = :state", params) />
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.44
White Master
Replace with
a graphic
5.5” Tall & 4.3” Wide
®
Copyright 2008 Adobe Systems Incorporated. All rights reserved. Adobe confidential.44
Any Questions
®
Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential.
45
45