Advanced orm

27
ight 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 1 ColdFusion Advanced ORM Himavanth Rachamsetty ColdFusion Engineer

description

 

Transcript of Advanced orm

Page 1: Advanced orm

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 1

ColdFusionAdvanced ORM

Himavanth Rachamsetty

ColdFusion Engineer

Page 2: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 2

Agenda

Advanced Mapping

CF-ORM Internals

Sessions &Transactions

Concurrency Control (Optimistic Locking)

Performance Optimization Lazy – Loading

Caching

Q & A

Page 3: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 3

Advanced Mapping

Collection Mapping

Embedded Mapping

Join Mapping

Inheritance Mapping

Page 4: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 4

Collection Mapping

Similar to 1:n relationship

Useful when target table need not be mapped as persistent CFC

Just need information from the target table

Demo

Page 5: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 5

Embedded Mapping

One entity with multiple CFC

Mapping needs to be specified in *.hbmxml

<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>

Page 6: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 6

Join Mapping

Useful when using one CFC for multiple tables

<cfcomponent persistent="true“ table=“employee”> <cfproperty name="id"> <cfproperty name="name"> <cfproperty name="city"

table="Address” joincolumn="addressId"> <cfproperty name="country"

table="Address“ joincolumn="addressId">

</cfcomponent>id name

1 John

addressId

city country

1 Melbourne

Australia

Page 7: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 7

Inheritance Mapping

Three Types

Table per hierarchy

Payment

+ID+Amount+Date

CreditCardPayment

+CardNo+CardType

ChequePayment

+ChequeNo+bankName+City

ID <<PK>>AmountDatePaymentType (discriminator)CardNoCardTypeChequeNoBankNameCity

Payment Table

Page 8: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 8

Inheritance Mapping

Demo

Page 9: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 9

Inheritance Mapping

Three Types

Table per hierarchy

Table per subclass (without discriminator)

Payment

+ID+Amount+Date

CreditCardPayment

+CardNo+CardType

ChequePayment

+ChequeNo+bankName+City

ID <<PK>>AmountDate

Payment Table

PaymentIDCardNoCardType

CreidtCardPayment Table

PaymentIdChequeNoBankNameCity

Cheque Payment Table

Page 10: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 10

Inheritance Mapping

Demo

Page 11: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 11

Inheritance Mapping

Three Types

Table per hierarchy

Table per subclass (without discriminator)

Table per subclass (with discriminator)

Payment

+ID+Amount+Date

CreditCardPayment

+CardNo+CardType

ChequePayment

+ChequeNo+bankName+City

ID <<PK>>AmountDatePaymentType (discriminator)

Payment Table

PaymentIDCardNoCardType

CreidtCardPayment Table

PaymentIdChequeNoBankNameCity

Cheque Payment Table

Page 12: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 12

Inheritance Mapping

Demo

Page 13: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 13

Application Start

ORM Enable

d?

Proceed with other

activities

false

true

Create/Load Hibernate

configuration if specified

Load Hibernate mapping files (*.hbmxml)

Search for persistent CFCs

Generate Hibernate

Mapping for persistent CFCs

DatabaseInspect

Generate DDL based on dbcreate

Build Hibernate Session Factory

Proceed with other activities

ORM Internals - Initialization

Page 14: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 14

ORM Session

All the ORM operations happen in a session

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

Provides First Level Caching

Automatically managed by CF In most cases, you don’t need to worry about it

A unit of work – a transaction

Page 15: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 15

ORM Session Management

New Request

New ORM Session

Call ORMFlushClose Session

Batch all the operations

Application Start

Page 16: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 16

Object States

Transient

Persistent

Detached

EntityNew()/ new()/ CreateObject

Transient Entity

Persistent Entities

EntitySave()

DB

Detached

Session ends

EntityLoadORMExecuteQuery

EnityMerge

EntityDelete

EntitySave

Session

Page 17: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 17

ORM Session Management – Transactions

17

New Request

New ORM Session

Application Start

Call ORMFlush

Call ORMFlushClose ORM Session

Batch all the operations

Page 18: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 18

Concurrency Control

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= newValwhere col1= oldVal and col2= oldVal2

Dirty

Includes only modified fields in the current session

Update myTbl set col1= newVal where col1= oldVal

Version (default)

Checks only version or timestamp column

<cfproperty name="lastModified“ fieldtype="timestamp|version“>

None

Also available at property level. <cfproperty .. optimisticlock=“true”>

Page 19: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 19

Fetching Strategy

Lazy fetching Default strategy, lazy=true

Extra Lazy : lazy = “extra” gets pk of arts and then all art columns from db

Immediate fetching Fetch target relationship in a separate SQL, immediately

<cfproperty name=“artist" fieldtype="one-to-many" cfc=“art" fkcolumn=“ARTISTID“ lazy="false"

fetch="select">

Eager fetching Fetch together in a single SQL, fetch=“join”

Useful for 1:1 frequently used relationships

Batch fetching < cfcomponent persistent="true" batchsize="100“ >

When fetching relationship, get some more that maybe required later

Get Addr1 for emp101, plus address for emp102, 103 etc depending on batch size

Page 20: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 20

Caching

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

EntyReload re-fetches

Page 21: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 21

Secondary Level Cache

Caches data across sessions

In-memory/disk/clustered

Pluggable cache impls

Default – EHCache

Secondary Cache

Page 22: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 22

Secondary Level caching ...

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

What is being cached

How (caching strategy)

3 Things in 4 Ways

Page 23: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 23

Secondary Level Cache – 3 Things in 4 Ways

3 Things Component

cfcomponent

Collection/Association cfproperty

Query EntityLoad/OrmExecuteQuery

4 Ways (cacheuse)

Read-only

Nonrestrict-read-write

Read-write

Transactional

Page 24: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 24

Caching - examples

Component <cfcomponent persistent="true“

cachename=“foo_region" cacheuse="read-only">

Relationship/Association/Collection Primary Key of the associated object is cached

The associated object itself is cached if specified as below

<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"});

Page 25: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 25

Caching – cacheuse

Read-only Best performance for read only data

Read-write Use if your data needs to be updated

More overhead

Nonrestrict-read-write Use when data is updated occasionally

Transactional Transactional cache

Can only be used if the cache provider is transaction aware

Page 26: Advanced orm

®

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 26

Caching - cleanup

ORMEvictEntityORMEvictEntity("<component_name>", [primarykey])

ORMEvictCollectionORMEvictCollection("<component_name>", "<relation_name>",

[primarykey])

ORMEvictQueriesORMEvictQueries([cachename])

Page 27: Advanced orm

Copyright 2009 Adobe Systems Incorporated. All rights reserved. Adobe confidential. 27

Thank You !

- [email protected]