Battlefield ORM : Learn the strategies and tactics to win with ColdFusion ORM powered by Hibernate!
We have gone through the pain and frustrations of maturing with technology such as an Object Relational Mapper (ORM) powered by Hibernate. This advanced session will cover how to leverage the ColdFusion ORM to start creating amazing, fun, and smell-great applications, and you might even see flying unicorns as well.
We will cover ORM session management, ORM settings, caching strategies, virtual service layers, dynamic finders, dynamic counters, and an enhanced Hibernate Criteria builder for ColdFusion to create easy and programmatic HQL queries. We will even see how to build automatic CRUD APIs using only your ORM definitions.
Ready to win?
3. CFCASTS
by Ortus Solutions
Learn from the makers of Coldbox, TestBox, and CommandBox through bite-size video tutorials on everything
you need to modernize your CFML development.
Become a CFML master!
Learning
Paths
Real-World
Examples
Engaging and
Interactive
Regularly
Updated Content
Flexible
Learning
Extensive
Course Library:
Watch all new series as they
are released
Access to the entire back catalog
You get 1 month for FREE
YEARLY
$275
Buy series a-la-carte
Access to free videos
Upgrade easily at any time
COMMUNITY
FREE
Watch all new series as they
are released
Access to the entire back catalog
Support Open Source Software
MONTHLY
$25
Why Choose CFCasts?
5. ➡ How to use ORM
➡ When to use ORM
➡ Tips & Trips to master Hibernate ORM
➡ Pitfalls to avoid
➡ Architectural overviews of ORM
➡ Extend ORM
Agenda
6. ➡ Object Relational Mapper
➡ Maps:
- Objects to tables
- Properties to columns
- Relationships to foreign keys and tables
➡ An easier way to persist data vs. boring CRUD SQL
➡ Focus on objects instead of data
➡ ColdFusion ORM (Abstraction to Hibernate/JPA)
➡ Almost Everything in Hibernate works in CF
ORM
What is
?
7. ➡ Write less boilerplate boring SQL
➡ Increase in productivity
➡ Rich Object Models
➡ Increased Flexibility
➡ Database vendor abstraction
➡ OO instead of query-based
➡ but…..
BENEFITS
8. Is it a Silver Bullet?
➡ Just another tool
➡ Times you need the power of the database: reports, legacy, sp, etc.
➡ Mix and Match
➡ There is a learning curve, but it is worth the investment
➡ What if you wanted an array of structs, list queries, or data arrays?
Use it Wisely!
13. BAD
DEFAULTS
Control Data Flushing + Transaction Borders:
ormsettings.flushAtRequestEnd = false
ormsettings.autoManageSession = false
Control Database Dialects (If Possible)
ormsettings.dialect = “MySQLwithInnoDB”
14. BAD
DEFAULTS
Control Entity Discovery or pay the price!
ormSettings.cfclocation = [ “/models”, “/ext/entities” ]
Great for debugging, NOT for production!
ormSettings.logSql = false
15. Need
HELP
Need even more debugging?
- Build out the Hibernate MXML
- Show syntax exceptions
ormSettings.saveMapping = true
ormSettings.skipCFCWithError = false
22. ➡ ORM relationship modeling is key
➡ OO is required
➡ UML is your best friend
➡ STOP THINKING ABOUT DATA
➡ YOU ARE NOT MODELING A DATABASE
OO Modeling is Key
27. ➡ Unidirectional or Bidirectional
➡ Collections can be as arrays or structs
➡ Filtered Collections (Missed by devs)
➡ Performance Failures
➡ Control when they are loaded (lazyness)
➡ Control how many related entities are loaded
➡ Executing code on loading
➡ The hardest part of ORM
RELATIONSHIPS
28. ➡ one-to-many, many-to-many
➡ add<relationship_property_name>(<associated_object>)
➡ boolean remove<relationship_property_name>(<associated_object>)
➡ boolean has<relationship_property_name>(<associated_object>)
➡ many-to-one, one-to-one
➡ boolean has<relationship_property_name>()
• addArts( Art art )
• removeArts( Art art )
• hasArts()
• hasArts( Art art )
property name=“arts"
fieldtype="one-to-many"
cfc="Art"
fkcolumn=“ArtistID";
GENERATED
METHODS
30. ➡ Immediate Fetching (Default)
➡ select with left outer join
➡ Use lazy For ALL Relationships or pay the price
YOU HAVE TO BE
LAZY
31. ➡ Three types of laziness values:
lazy=“true”
all relationships
Loads the relationship data when the getter is called (Batches, if used)
lazy=“extra”
one-to-many, many-to-many
Loads proxy light objects with primary keys
(Event better - Batches, if used)
lazy=“proxy”
one-to-one, many-to-one
Loads proxy with primary key (Same as above)
YOU HAVE TO BE
LAZY
32. ➡ Eager Fetching
➡ Mostly used in one-to-one and many-to-one, but applies to all
➡ Default uses 2 SQL queries, reduce to 1 Query
property name=“role” fieldtype=“many-to-one” fetch=“join”;
➡ Batch Fetching
➡ Limits the way relationships are loaded, else Hibernate tries to load all records
➡ Used on many-to-many and one-to-many collection properties:
property name=“comments” fieldtype=“one-to-many” batchsize=“10” lazy=“extra”;
➡ Used at entity level as well:
component name=“Comment” batchsize=10{}
YOU HAVE TO BE
LAZY
33. Oracle Tip
➡ JDBC Fetch Sizes are defaulted to 10
➡ Slow for many records or batch operations
➡ Increase the JDBC Fetch size
➡ Custom Hibernate setting
hibernate.jdbc.fetch_size=100
YOU HAVE TO BE
LAZY
34. ➡ They can be more of a headache
➡ Cascading Deletes are painful
➡ Choose the controlling relationship
➡ inverse=true
➡ Else double queries, inefficient queries
➡ Does it make sense?
➡ Supporting methods for bi-directional linkage
➡ Supporting methods for un-linkages
AVOID BI-DIRECTIONAL,
IF POSSIBLE!
40. ➡ Not the same as session scope
➡ A transitionary space + Caching Layer
➡ entityLoad()
➡ entityNew() ?
➡ You need to control when to send to DB
➡ transaction{}, ormflush()
➡ You can remove entities from it and clear it
➡ ORMClearSession()
➡ ORMEvictEntity(), ORMEvictCollection()
➡ ORMCloseSession()
UNDERSTAND THE HIBERNATE SESSION
41. Hibernate Session
(Conversation - Request)
DB
Eu
Ex Ez
Ey
Cache
ehCache/
Couchbase
Batched SQL
EntityNew()
EntityLoad()
Data (!CFCs)
Hibernate Session
Factory (application)
CRUD
When?
ORMClearSession()
UNDERSTAND THE HIBERNATE SESSION
43. ➡ Transactions demarcate SQL boundaries
➡ Important Imperative for ORM + SQL
➡ No communication to DB should occur without one
➡ Reactive programming, expect the worst
➡ cftransaction or Hibernate raw transactions
TRANSACTION DEMARCATION
44. ➡ Transaction Theory:
➡ Any existing ORM session is flushed and reused
➡ Data can be committed or rollback
➡ ORMFlush() is ignored in transactions
➡ If commit, then flushed to database
➡ If rollback, session is cleared
TRANSACTION DEMARCATION
47. ➡ Don’t do it!
➡ Breaks the link to the Hibernate Session
➡ Relationships will fail if not lazy loaded
➡ entityMerge()
➡ Store ID’s or data instead, then inflate the entities
AVOID SCOPING ENTITIES
49. ➡ Don’t go cache crazy, develop a strategy
➡ Misconception: Does not store CFC
➡ Stores individual property values
➡ Use distributed caches: ehcache, couchbase, redis
➡ You can cache:
➡ Entity property data : Only caches properties data values
➡ Entity association data : Only caches primary keys
➡ Query data : HQL, ORMExecuteQuery()
➡ Evictions:
➡ ORMEvictEntity(), ORMEvictCollection()
CACHE
BOOSTING
52. ➡ The fastest gun in the galaxy!
➡ Return array of structs
➡ Why a full ORM Object graph?
➡ Boost your APIs, stop converting queries/
array of objects to JSON
HQL
MAPS
55. When is HappyBox?
ORM was fast
Extensible way to finish
the remaining 20%
CF ORM was easier to use?
80% of API Querying
OO way to query
Auto build relationships
ORM
THOUGHTS?
56. Base ORM Service
Virtual ORM Service
Active Entity
Entity Populators
Validation
Event Handlers
DI/AOP
OVERVIEW
57. ➡ Service layer for any entity
➡ OO Querying, caching, transactions
➡ Dynamic finders, getters, counters
➡ Object metadata & session management
➡ Exposes more features from Hibernate
➡ 90% Foundation
BASE ORM
SERVICE
58. ➡ Extends Base ORM Services
➡ Roots itself to a single entity = Less Typing
➡ You can build the 10%
VIRTUAL
SERVICES
59. ➡ Active Record Pattern
➡ Sweet validation integration
➡ DI/AOP Available
ACTIVE
ENTITY
63. BASE ORM
SERVICE
Dynamic Finders/Counters
➡ Expressive Programming
➡ Three types of dynamic Finders/Counters
➡
fi
ndBy :
fi
nd ONE entity
➡
fi
ndAllBy :
fi
nd ALL entities
➡ countBy: Give you a count
64. BASE ORM
SERVICE
Dynamic Finders/Counters
➡ Conditionals
➡ LessThanEquals, LessThan
➡ GreaterThanEquals, GreaterThan
➡ Like
➡ Equal, NotEqual
➡ isNull, isNotNull
➡ Between, NotBetween
➡ inList, notInList
➡ Operators
➡ And
➡ Or
➡ Query Options
➡ ignoreCase, timeout, max, offset
➡ cacheable, cachename
66. CRITERIA
BUILDER
➡ Limitations of CF ORM:
➡ entityLoad() has limited features
➡ Some operations we always need an entity =
slow
➡ What if I want arrays, or arrays of structs
➡ Complex relationships are hard to query
➡ SQL/HQL string build is so 90’s == NOT FUN!
67. CRITERIA
BUILDER
➡ Programmatic DSL Builder
➡ Rich set of criterias
➡ Projections and Result transformations
➡ Subqueries
➡ Caching
➡ SQL Inspections & Debugging
➡ Array of structures is twice as fast as
queries
69. CRITERIA
BUILDER
➡ Request new criteria
➡ newCriteria()
➡ Add simple restriction(s)
➡ Find all cars sold between April and July
➡ Use between()
➡ Get results
➡ Use list( max, offset, timeout, sortOrder, ignoreCase, asQuery )
➡ Get counts
➡ Use count()
72. CRITERIA
BUILDER
ALIASES-> JOINS
➡ Allows you to do queries within relationships
➡ Creates SQL Joins
➡ Aliases can be nested, so if your entity knows
about it, you can query it!
73. CRITERIA
BUILDER
PROJECTIONS
➡ Projects change nature of results
➡ Arrays of data, or arrays of structs (Mighty Fast)
➡ Once its added its there forever
➡ avg
➡ count
➡ countDistinct
➡ distinct
➡ groupProperty
➡ max
➡ min
➡ property
➡ sum
➡ rowCount
➡ id
➡ sqlProjection
➡ sqlGroupProjection
➡ detachedSQLProjection