Advanced ColdFusion ORM

Comments

There aren't any comments for this presentation.

Add Comment

Please enter a valid email address.

Optional. Enter a URL for your website.

  Remember Me
  Notify me of follow up comments

Transcript

no image

Slide Text

Slide Notes


Advanced ColdFusionORM adobemax348

no text exists for this slide

no notes exist for this slide

Agenda


ORM Basics
ORM Basics
Demo
Advanced Mapping
What happens internally
Transaction & concurrency control
Caching & optimization
Event Handling
Auto-generation of tables
Q & A

no notes exist for this slide

ORM Basics


Object - Relational Mapping
Object - Relational Mapping
A persistence framework to map objects to the relational database without writing SQL.
A programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages.
ColdFusion ORM – based on Hibernate
One of the most mature and popular persistence framework

no notes exist for this slide

ColdFusion ORM

no text exists for this slide

no notes exist for this slide

Database Model Employees

no text exists for this slide

-> Simple Property Mapping -> Over-riding the column Name -> Specifying the ormtype

Slide 6

no text exists for this slide

Show simple Mapping of Employee Show CRUD code of Employee Show non-persistent properties Show computed properties Show one-to-many relationship with array Show many-to-one Show CRUD on one-to-many relationship Show cascade operation Show logSQL Show ormreload Show one-to-many relationship with map Show one-to-one relationship Show many-to-many relationship

Advanced Mapping


Collection Mapping
Collection Mapping
Inheritance Mapping
Embedded Mapping
Join Mapping
Using Hibernate mapping files directly

no notes exist for this slide

Collection Mapping


Similar to 1:n relationship
Similar to 1:n relationship
Useful when target table need not be mapped as persistent CFC
Just need information from the target table
Mapping defined similar to 1:n relationship
Demo

no notes exist for this slide

Inheritance Mapping


Three Types
Three Types
Table per hierarchy

no notes exist for this slide

Slide 10


Three Types
Three Types
Table per hierarchy
Table per subclass

no notes exist for this slide

Slide 11


Three Types
Three Types
Table per hierarchy
Table per subclass
Table per subclass with discriminator

no notes exist for this slide

Embedded Mapping


One entity with multiple CFC
One entity with multiple CFC
A cfproperty refers to another cfc
Mapping needs to be specified in *.hbm.xml
<hibernate-mapping>
<class name="cfc:Employees" entity-name="employee" table="Employees">
<id name="ID" type="integer" column="EmployeeID">
<generator class="native"/>
</id>
<component name="Name" class="cfc:cname">
<property name="FirstName" type="string" column="FirstName"/>
<property name="LastName" type="string" column="LastName"/>
</component>
<property name="Title" type="string" column="Title"/>
<property name="BirthDate" type="date" column="BirthDate"/>
<property name="HireDate" type="date" column="HireDate"/>
</class>
</hibernate-mapping>

no notes exist for this slide

Join Mapping


Useful when using one CFC for multiple tables
Useful when using one CFC for multiple tables
<cfcomponent persistent="true“ table=“Person”>
<cfproperty name="id">
<cfproperty name="name">
<cfproperty name="city"
            table="Address” joincolumn=“personId">
<cfproperty name="country"
            table="Address“ joincolumn=“personId"> </cfcomponent>

no notes exist for this slide

ORM Initialization

no text exists for this slide

no notes exist for this slide

ORM Session


Represents a unit of work – typically a transaction
Represents a unit of work – typically a transaction
All the ORM operations happen in a session
Provides First Level Caching
Ensures a single instance of an entity.
Tracks changes made to the objects
SQLs are executed when session is flushed
Typically when the transaction is committed or when the request completes
Can use ORMFlush to force
Automatically managed by CF
In most cases, you don’t need to worry about it

no notes exist for this slide

ORM Session Management

no text exists for this slide

no notes exist for this slide

ORM Session Management Transactions

no text exists for this slide

CFTransaction tag is fully supported in ColdFusion-ORM Savepoint and rollback are supported Isolation-levels: Isolation level, which indicates which type of read can occur during the execution of concurrent SQL transactions. The possible read actions include dirty read, in which a second SQL transaction reads a row before the first SQL transaction executes a COMMIT; non-repeatable read, in which a SQL transaction reads a row and then a second SQL transaction modifies or deletes the row and executes a COMMIT; and phantom, in which a SQL transaction reads rows that meet search criteria, a second SQL transaction then generates at least one row that meets the first transaction's search criteria, and then the first transaction repeats the search, resulting in a different result set. read_uncommitted: Allows dirty read, non-repeatable read, and phantom read_committed: Allows non-repeatable read and phantom. Does not allow dirty read. repeatable_read: Allows phantom. Does not allow dirty read or non-repeatable read. serializable: Does not allow dirty read, non-repeatable read, or phantom.

Object States


Transient
Transient
Persistent
Detached

no notes exist for this slide

Concurrency Control


Optimistic lock for high concurrency
Optimistic lock for high concurrency
Update only if the entity is not modified by other thread or externally.
optimisticlock attribute on cfc
All
All properties are included in where clause of update
      Update myTbl set col1= newVal, col2= newVal2
         where col1= oldVal and col2= oldVal2
Dirty
Includes only modified fields in the current session
Version (default)
Checks only version or timestamp column
<cfproperty name="lastModified“ fieldtype="timestamp|version“>
None
You can also choose the property for dirty check.

no notes exist for this slide

Fetching Strategy


Immediate fetching
Immediate fetching
Fetch target relationship in a separate SQL, immediately
<cfproperty name=“emp" fieldtype="one-to-many" cfc=“order"           fkcolumn=“EMPID“ lazy="false" fetch="select">
Lazy fetching
Default strategy, lazy=true
On demand, fetch related entities
Lazy = “extra” gets pk of orders and then all order columns from db
Eager fetching
Fetch together in a single SQL, fetch=“join”
Useful for 1:1 frequently used relationships
Batch fetching
When fetching relationship, get some more that maybe required later
Get Addr1 for emp101, plus address for emp102, 103 etc from the table depending on batch size

no notes exist for this slide

Caching


Session Level
Session Level
Ensures a single instance for a given ID
EntityLoad fetches data for the first time
Data is cached for the session
Next request in the same session will use cached data
EntityReload re-fetches
Secondary Level Cache

no notes exist for this slide

Secondary Level Cache


Caches data across sessions
Caches data across sessions
In-memory/disk/clustered
Pluggable cache impls
Default - EHCache
Component Caching
Collection Caching
Query Caching

no notes exist for this slide

Secondary Level caching


Configuration in Application.cfc
Configuration in Application.cfc
ormsettings.secondarycacheenabled
ormsettings.Cacheprovider
JBossCache, OSCache, SwarmCache, Hashtable, DEFAULT - ehcache
ormsettings.cacheconfig
Appropriate config file for cache, e.g. ehcache.xml
In ORM cfc
“cacheuse” defines caching strategy
“cahcename” cache region, a bucket for this data

       <cfcomponent persistent="true“
          cachename=“foo_region" cacheuse="read-only">

no notes exist for this slide

Caching examples


Component
Component
<cfcomponent persistent="true“
          cachename=“foo_region" cacheuse="read-only">
Relationship
Primary Key of the associated object is cached
The associated object itself is cached if coded as above
<cfproperty name="arts" fieldtype="one-to-many“           cachename="foo_region" cacheuse="read-write">
Query data
   ORMExecuteQuery("from Art where issold=0", {}, false,       {cacheable=true, cachename=“foo_region"});

no notes exist for this slide

Caching cacheuse


Read-only
Read-only
Best performance for read only data
Nonrestrict-read-write
Use when data is updated occasionally
Read-write
Use if your data needs to be updated
More overhead than the two preceding strategies
Transactional
Transactional cache
Can only be used if the cache provider is transaction aware

no notes exist for this slide

Caching cleanup


ORMEvictEntity
ORMEvictEntity
ORMEvictEntity("<component_name>", [primarykey])
ORMEvictCollection
ORMEvictCollection("<component_name>", "<relation_name>",              [primarykey])
ORMEvictQueries
ORMEvictQueries([cachename])

no notes exist for this slide

Event Handling


Set ormsettings.eventhandling=“true”
Set ormsettings.eventhandling=“true”
CFC level
preinsert and postinsert
predelete and postdelete
preupdate and postupdate
preload and postload
Application Level
Set ormsettings.eventhandler=“AppEventHandler.cfc”
Should implement the CFIDE.orm.IEventHandler interface

no notes exist for this slide

Autogenerating tables


Tables created on Application startup
Tables created on Application startup
ormsettings.dbcreate
Update – create new or update if table exists
Dropcreate – drop and then create table
None – do nothing
ormsettings.sqlscript
Executes the sql script at the time of table creation.

no notes exist for this slide

Naming Strategy


Defines the strategy for naming tables and columns
Defines the strategy for naming tables and columns
ormsettings.namingStrategy
Default – CFC/property name matches table/column name
Smart – camel cased name is uppercased with “_” between words.
CFC “OrderProduct” is “ORDER_PRODUCT”, OrderID is “ORDER_ID”
Your own CFC that implements cfide.orm.INamingStrategy
Component UCaseStrategy implements cfide.orm.INamingStrategy
{
public string function getTableName(string tableName)
{
return Ucase(tableName);
}
public string function getColumnName(string columnName)
{
return Ucase(columnName);
}
}

no notes exist for this slide

Slide 30

no text exists for this slide

no notes exist for this slide

Slide 31

no text exists for this slide

no notes exist for this slide