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.

Gorm Inside and Out

1,770 views

Published on

GORM is a super powerful ORM tool that makes ORM simple by leveraging the flexibility and expressiveness of a dynamic language like Groovy. With GORM developers get access to all of the power and flexibility of an ORM tool like Hibernate without any of the complexity.

Published in: Software
  • Be the first to comment

Gorm Inside and Out

  1. 1. GORM Inside And Out Jeff Scott Brown @jeffscottbrown © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  2. 2. ORM Re-invented • Grails provides a “convention-over-configuration” configuration strategy • Uses the convention in the class names, property names and property types to provide mapping • All this information is already there in the JVM! 2
  3. 3. hasMany Mapping • hasMany is a map of one-to-many relationships. Example: ! ! • A property of type java.util.Set is automatically created static hasMany = [comments:Comment] bookmark.comments?.each { println it.text } 3
  4. 4. belongsTo Mapping • The belongsTo mapping is important in that it states: • "I belong to X and therefore when X is deleted so am I" class Comment { ... static belongsTo = Bookmark } 4
  5. 5. Lists, Sets & Maps • GORM supports mappings of Lists and Maps using an index column ! ! class ! Bookmark { class Bookmark { List comments Map commentsByUser ! static hasMany = static hasMany = [comments:Comment] [commentsByUser:Comment] } ! } ! • Default behavior is to assume a collection is a Set, but you can use SortedSet too! 5
  6. 6. Advanced Mapping • GORM features a ORM mapping DSL for more advanced mappings • With access to features like: • Optimistic locking control • Second level cache Config • Inheritance strategy (table-per-subclass?) • Id generation strategy (hilo, sequence, native ..) • Composite Ids • Eager/Lazy fetching • Table/Column name & type mappings 6
  7. 7. Relationship Management • GORM automatically adds handy relationship management methods 7 new Bookmark(url:"http://grails.org") .addToComments(text:"Great!", leftBy:"Joe") .addToComments(text:"Groovy!", leftBy:"Fred") .save() // and later bookmark.removeFromComments(myComment)
  8. 8. Domain Constraints • Constraints are settings applied against properties of the domain • They allow: • Additional declarative validation • Control of database schema generation • Control of scaffolded view generation (CRUD) • Constraints are defined with a Groovy builder 8
  9. 9. Constraints in Action 9 class Bookmark { URL url String title Date dateCreated = new Date() static constraints = { url(nullable:false) title(blank:false, size:1..255) dateCreated(nullable:false) } }
  10. 10. Querying • GORM supports a number of ways to query including: • Dynamic Finders • Criteria • Query-by-example • HQL Put image here! 10
  11. 11. Dynamic Finders • Automatically translate the properties of the class into "method expressions" - at runtime! • Uses the Hibernate Criteria API underneath • Rich and expressive way to query 11
  12. 12. Dynamic Finders 12 def all = Bookmark.list() // user like expressions def grailsBookmarks = Bookmark.findAllByTitleLike("%Grails%") // query between two values def now = new Date() def lastWeeks = Bookmark.findByCreatedDateBetween(now-7, now) // query associations def bookmark = Bookmark.get(34) def comments = Comment.findAllByBookmark(bookmark)
  13. 13. Querying with Criteria 13 // returns first 10 users who have an active // account that has been created in the last // 30 days and that have Grails-like // bookmarks created in the last 7 days def now = new Date() def users = Bookmark.withCriteria { comments { like("text","%Grails%") between("dateCreated", now-7, now) } between("dateCreated",now-30, now) maxResults(10) }
  14. 14. Querying with HQL • If all else fails, there is always HQL!: 14 // Query for Bookmark instances def bookmarks = Bookmark.findAll("from Bookmark b where b.title like ?", ["%Grails%"] ) // select only the Bookmark titles def titles = Bookmark.executeQuery( "select b.title from Bookmark b where b.title like ?", ["%Groovy%"] )
  15. 15. Q & A 15

×