Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ITB2017 - Slaying the ORM dragons with cborm

267 views

Published on

Slaying the ORM dragons with cborm

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ITB2017 - Slaying the ORM dragons with cborm

  1. 1. Slaying the ORM Dragons with CBORM
  2. 2. WHO AM I? • Luis Majano - Computer Engineer • Imported from El Salvador-----------> • Architecture + Software Design • CEO of Ortus Solutions • Adobe Community Professional • Creator of all things Box: 
 ColdBox, ContentBox, CommandBox,WireBox….
  3. 3. AGENDA • ORM FirstThoughts • Is ORM a Silver Bullet? • Visit from a Special Guest • Specialized ORM Services
  4. 4. ORMThoughts? CF ORM was easier to use? OO way to query ORM was fast 80% of API Querying Extensible way to finish the remaining 20% When is HappyBox? Auto build relationships
  5. 5. CONCLUSIONS • Can’t or doesn’t exist • Sounds like bull….. • What’s this latino up to? • Is he trying to get my money PROVE IT!
  6. 6. ORM is NOT a silver bullet • Just another tool • Times you need the power of the database: reports, legacy, sp, etc. • Mix and match • What if you wanted array of structs, or lists or queries or arrays of data? • There is a learning curve, but it is worth the investment
  7. 7. Applied Benefits • Increase in productivity of about 40% • Rich Object Models • Deliver DB patching updates • Increased Flexibility • Great for abstracting vendor specific intricacies • OO Queries • Avg JDBC <10ms of about 350K Transactions
  8. 8. How do we start? coldfusionormbook.com amazon
  9. 9. Guru ORMLui 10 keys to ORM Success!
  10. 10. #1: OO Modeling is Key • ORM relationship modeling is key • OO is required • UML is your best friend • STOPTHINKING ABOUT DATA • YOU ARE NOT MODELING A DATABASE
  11. 11. #1: OO Modeling is Key
  12. 12. #1: OO Modeling is Key Query Object Data Data + Behavior
  13. 13. #2: Bad Engine Defaults Do not use engine defaults: Control Data Flushing +Transaction Borders: 
 ormsettings.flushAtRequestEnd=false ormsettings.autoManageSession=false Control Database Dialects (If Possible) ormsettings.dialect=“MySQLwithInnoDB”
  14. 14. #2: Bad Engine Defaults • this.ormsettings.cfclocation • Use it to a specified directory, else pay the price of discovery • this.ormsettings.logSQL=true • Great for debugging, false for production, else pay the price • Having Issues? • Hibernate MXML: this.ormsettings.saveMapping=true • DataBoss - ortussolutions.com/products/databoss
  15. 15. #3: Understand Hibernate Session • 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()
  16. 16. #3: Understand Hibernate Session Hibernate Session 
 (Conversation - Request) DB Eu Ex EzEy Cache ehCache/ Couchbase Batched SQL EntityNew() EntityLoad() Data (!CFCs) Hibernate Session Factory (application) CRUD When? ORMClearSession()
  17. 17. #3: Understand Hibernate Session DB Sync Insertions in order updates Collection deletions collection deletion, updates, inserts collection insertions deletions in order
  18. 18. #4:Transaction Demarcation • 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 transactions
  19. 19. #4:Transaction Demarcation • 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
  20. 20. #5 Lazyness • Immediate Fetching (Default) • select with left outer join
 poor, poor, poor, poor performance • Use lazy For ALL Relationships or pay the price • Three types of laziness: • lazy=“true” • all relationships • Loads all the relationship data when called upon (Batch sizes if used) • lazy=“extra” • one-to-many, many-to-many • Loads proxies with primary keys (Event better - Batch sizes if used) • lazy=“proxy” • one-to-one, many-to-one • Loads proxy with primary key
  21. 21. • 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{} #5 Lazyness
  22. 22. 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 #5 Lazyness
  23. 23. #6:Avoid bi-directional • They can be more of a headache • Cascading Deletes • Choose the master relationship • inverse=true • Else double queries • Does it make sense? • Supporting methods for bi-directional linkage • Supporting methods for un-linkages
  24. 24. #6:Avoid bi-directional
  25. 25. #6:Avoid bi-directional
  26. 26. #7: Do not store entities in scopes • Don’t do it! • No linkage to Hibernate Session • Relationships will fail if not lazy • entityMerge() • Store ID’s instead
  27. 27. #8: Use DB Indexes • #1 Performance Problem • Identify relationships • Identify HQL, SQL • property name=“isActive” index=“idxActive”
  28. 28. #9: Cache = BOOST! • Don’t go cache crazy • Develop a strategy • Does not store CFC, stores individual property values • Use distributed caches: ehcache, couchbase • 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()
  29. 29. #10: HQL Maps select new map( 
 max( bodyWeight ) as max, 
 min( bodyWeight ) as min, 
 count( * ) as n ) from Cat cat • Return array of structs • Why a full ORM Object graph? • Boost your APIs, stop converting queries/array of objects to JSON
  30. 30. ORM Module install cborm
  31. 31. ORM Module Base ORM Service Virtual ORM Service Active Entity Entity Populators Validation Event Handlers
 DI/AOP
  32. 32. Base ORM Service • Service layer for any entity • OO Querying, caching, transactions • Dynamic finders, getters, counters • Object metadata & session management • Exposes more features from Hibernate • 90% Foundation
  33. 33. • Extends Base ORM Services • Roots itself to a single entity = LessTyping • You can build the 10% Virtual/Concrete ORM Services
  34. 34. • Active Record Pattern • Sweet validation integration • DI/AOP Available Active Entity
  35. 35. • Populate Entities: xml, json, queries, structs • Compose relationships from simple values • Null support • Exclude/include fields • Server side validation • Dependency Injection Listeners • Custom Event Driven Programming Entity Populators Validation Event Handlers ORM Utilities
  36. 36. ORM Services in Action box install cartracker-demo
  37. 37. Base ORM Service • count(), countWhere() • delete(), deleteAll(), deleteByID(), deleteByQuery(), delete Where() • evict(), evictEntity(), evictQueries() • executeQuery(), list() • exists() • findAll(), findAllWhere(), findByExample(), findIt(), findWhere() • get(), getAll(), • getKey(), getPropertyNames(), getSessionStatistics(), getTableName() • clear(), merge(), new(), refresh() • populate(), populateFromJSON(), populateFromXML(), populateFromQuery() • save(), saveAll()
  38. 38. Base ORM Service Dynamic Finders/Counters • Expressive Programming • Three types of dynamic Finders/Counters • findBy : find ONE entity • findAllBy : find ALL entities • countBy: Give you a count
  39. 39. 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
  40. 40. Criteria Builder Awesome OO Queries
  41. 41. 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!
  42. 42. 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
  43. 43. Criteria Builder
  44. 44. 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()
  45. 45. Criteria Builder Restrictions • between() • eq() • gt() • ge() • gtProperty() • isEmpty() • isNull() • ne() • ilike() • and() • or() • not() • conjunction() • disjunction() • isTrue() • isFalse() • sqlRestriction() • ...much more!
  46. 46. Criteria Builder Retrievals • Retrieval • firstResult() • get() • list() • count() • Options • fetchSize() • readOnly() • maxResults() • cache(), cacheRegion() • timeout() • order()
  47. 47. 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!
  48. 48. 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
  49. 49. Criteria Builder Debugging + Logging • Criteria Builder SQL Inspector • startSQLLog( returnExecutableSQL, formatSQL ) • stopSQLLog() • getSQLLog() • getSQL( returnExecutableSQL, formatSQL )
  50. 50. Thanks! Q & A

×