![Page 1: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/1.jpg)
Building Grails Applications with PostgreSQL
Brent Baxter and Ken RimplePostgreSQL East - March 25, 2010
Tuesday, March 30, 2010
![Page 2: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/2.jpg)
PostgreSQL East - March 25, 2010
About Brent and Ken
• Brent Baxter: [email protected]
‣ Consultant and Applications Architect
‣ Grails, Java, and Spring developer
• Ken Rimple: [email protected]
‣ Head of Education Services
‣ Host, Chariot TechCast:
http://techcast.chariotsolutions.com
Tuesday, March 30, 2010
![Page 3: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/3.jpg)
PostgreSQL East - March 25, 2010
• Java and Open Source solutions provider
• Chariot Solutions Education Services
‣ Groovy and Grails training and mentoring
• Chariot Solutions Consulting
‣ Development and consulting services
• http://www.chariotsolutions.com
About Chariot Solutions
Tuesday, March 30, 2010
![Page 4: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/4.jpg)
PostgreSQL East - March 25, 2010
Goals
• Provide a basic overview of the Grails framework - with a dash of Groovy
• Review some of the features of Grails Object Relational Modeling (GORM)
• Demonstrate Grails with PostgreSQL
Tuesday, March 30, 2010
![Page 5: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/5.jpg)
PostgreSQL East - March 25, 2010
Introduction to Groovy
Tuesday, March 30, 2010
![Page 6: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/6.jpg)
PostgreSQL East - March 25, 2010
Groovy
• A dynamic language built for the Java Virtual Machine
‣ Compiles to native ByteCode
‣ Can inherit Groovy classes from Java classes (and vice versa)
‣ Native access to any Java library
Tuesday, March 30, 2010
![Page 7: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/7.jpg)
PostgreSQL East - March 25, 2010
Benefits of Groovy
• Adds "Convention over Configuration"
‣ Groovy beans are VERY simple
‣ Assumes methods are public, member variables are private
‣ Constructors not required
‣ Easy collections – Use the [ ] syntax for lists and maps
Tuesday, March 30, 2010
![Page 8: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/8.jpg)
PostgreSQL East - March 25, 2010
A Sampling of Groovy APIs
• xml – Powerful XML Parsing, Creation
• gsql – Simplified JDBC API
• Groovy Testing– simple testing API, mocking with closures, etc...
• Collections – lists, maps and ranges
• builders – a set of repetitive building tasks can be automated using various Groovy Builders
• Swing – the Groovy SwingBuilder makes UI easy. Also Griffon is a Groovy-based MVC framework for Swing (like Grails for Web)
Tuesday, March 30, 2010
![Page 9: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/9.jpg)
PostgreSQL East - March 25, 2010
Java Person Class
public class Person {private String firstName;private String lastName;private Date birthDate;
public Person(String firstName,String lastName, Date birthDate) {
...}
public void setFirstName() { ... } public String getFirstName() { ... }
// etc...}
Tuesday, March 30, 2010
![Page 10: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/10.jpg)
PostgreSQL East - March 25, 2010
Groovy Person Class
class Person {String lastNameString firstNameDate birthDate
}
Tuesday, March 30, 2010
![Page 11: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/11.jpg)
PostgreSQL East - March 25, 2010
Constructors and Lists
// Groovy provides constructors for freedef bob = new Person(firstName:'Bob', lastName:'Smith')def sue = new Person(firstName:'Sue', lastName:'Jones',
birthDate:new Date('8/12/1974'))
// ArrayLists are simpledef persons = [ new Person(firstName:‘Bob’),
new Person(firstName:‘Sue’)]
persons += new Person(firstName:‘John’)
Tuesday, March 30, 2010
![Page 12: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/12.jpg)
PostgreSQL East - March 25, 2010
Groovy SQL Support
// Using JDBC
def sql = Sql.newInstance(url, usr, pwd, driver)sql.execute(“insert into table values ($foo, $bar)”)sql.execute(“insert into table values (?,?)”, [a,b])sql.eachRow(“select * from USER”) {println it.name}def list = sql.rows(“select * from USER”)
Tuesday, March 30, 2010
![Page 13: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/13.jpg)
PostgreSQL East - March 25, 2010
Introduction to Grails
Tuesday, March 30, 2010
![Page 14: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/14.jpg)
PostgreSQL East - March 25, 2010
• A web application framework
‣ Three-tiered Model View Controller (MVC)
• Convention based
‣ Sensible default configuration or use any of a number of simple DSLs
• Extendable with a rich set of plug-ins
Grails Framework
Tuesday, March 30, 2010
![Page 15: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/15.jpg)
PostgreSQL East - March 25, 2010
Grails Is Groovy
• Leverages the Groovy programing language
‣ Groovy Services and Beans
• Configuration DSLs
‣ Logging, data source configuration, dependency resolution, ...
• Web tier Groovy Server Pages (GSP)
Tuesday, March 30, 2010
![Page 16: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/16.jpg)
PostgreSQL East - March 25, 2010
• Framework backed by best of breed industry standards
‣ Spring, Hibernate, SiteMesh, Web Flow, etc.
• Leverage any Java library, access from Groovy or Java classes
• Can be deployed as a Java web application (WAR) to any Servlet container
Grails Is Java
Tuesday, March 30, 2010
![Page 17: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/17.jpg)
PostgreSQL East - March 25, 2010
Grails Technology Stack
Groovy
Java API
Java Platform (JVM)
Grails
SiteMesh Spring Hibernate StandardLibraries
Tuesday, March 30, 2010
![Page 18: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/18.jpg)
PostgreSQL East - March 25, 2010
Key Grails Components
• Domain Class - A class representing an object in your domain (database table)
• Controller - A class that operates on URLs submitted to the web site
• View - A Groovy Server Page (GSP) designed to render the content based on a specific request
Tuesday, March 30, 2010
![Page 19: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/19.jpg)
PostgreSQL East - March 25, 2010
• Focus on the complexities of the domain first
• Create domain objects and their relationships first
• Dynamically 'scaffold' controllers and views for each domain class, validate domain objects and relationships
• When ready, finish by generating or coding all views, controllers, and tests
Domain Driven Design with Grails
Tuesday, March 30, 2010
![Page 20: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/20.jpg)
PostgreSQL East - March 25, 2010
• Dynamically generated views based on default templates
• No need to define a page before completing the data model
// Domainclass Person {String firstNameString lastNameDate birthDate
}
Grails Scaffolding
// Dynamic Scaffold Controllerclass PersonController { def scaffold = true}
Tuesday, March 30, 2010
![Page 21: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/21.jpg)
PostgreSQL East - March 25, 2010
More than a web framework...
• Eases development of every tier
‣ Integrated Groovy build system
‣ Integrated Groovy unit and integration test mechanism
‣ Simple ORM based on Hibernate
‣ Extensible plug-in system built on Spring
‣ Also simplifies Web MVC
Tuesday, March 30, 2010
![Page 22: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/22.jpg)
PostgreSQL East - March 25, 2010
Grails Object Relational Mapping (GORM)
Tuesday, March 30, 2010
![Page 23: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/23.jpg)
PostgreSQL East - March 25, 2010
GORM
• Grails Object Relational Mapping API
‣ Uses domain classes written in Groovy
‣ Injects methods into domain classes for load, save, update, and query data
‣ Backed by Hibernate and Spring
‣ Write Hibernate objects without all of the messy XML!
Tuesday, March 30, 2010
![Page 24: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/24.jpg)
PostgreSQL East - March 25, 2010
GORM Benefits
• Write your domain classes as POGOs
• Define validation via constraints
• Define relationships via simple belongsTo, hasMany mappings
• Easy to test framework, can use grails console or grails shell, even integration tests
• Finders make it easy to locate data
Tuesday, March 30, 2010
![Page 25: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/25.jpg)
PostgreSQL East - March 25, 2010
Grails Domain Classes
• Created with create-domain-class
• Simple Groovy Beans
‣ Each member variable represents a column in a table
‣ Implied version, primary key fields
‣ Can define relationships with other domains, validation constraints
Tuesday, March 30, 2010
![Page 26: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/26.jpg)
PostgreSQL East - March 25, 2010
Connecting to PostgreSQL
• Grails defines database connections for various environments (Development, Test, Production)
• Steps to configure your database
‣ Install your database driver
‣ Modify grails-app/config/DataSource.groovy
‣ Boot grails with grails run-app
Tuesday, March 30, 2010
![Page 27: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/27.jpg)
PostgreSQL East - March 25, 2010
Defining Constraints
• Checked automatically when saved or when validate() is called
• Tip: scaffold configuration follows constraint order for field order
class Customer {
static constraints = { firstName(maxLength: 15, blank: false) lastName(maxLength: 20, blank: false) registrationDate(nullable: false) preferredCustomer(default: true) awardsBalance(range: 0.0..5000.0) } String firstName String lastName Date registrationDate Boolean preferredCustomer BigDecimal awardsBalance}
Tuesday, March 30, 2010
![Page 28: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/28.jpg)
PostgreSQL East - March 25, 2010
Entity Relationships
• Grails supports all Hibernate relation mappings
‣ One to One
‣ One to Many
‣ Many to Many
‣ Parent/Child (Inheritence)
Tuesday, March 30, 2010
![Page 29: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/29.jpg)
PostgreSQL East - March 25, 2010
One to many relationship• Customer 'owns' accounts (cascade deletes, updates, saves)class Customer {
String firstName String lastName ...
static hasMany = [accounts:Account]}
class Account {
static belongsTo = [customer:Customer] String accountNumber}
// arbitrary but working example... flush for testing only...def customer = new Customer(firstName: "Dude", lastName: "WheresMyCar")def account = new Account(accountNumber:"1234", customer:customer)customer.accounts = [account]def result = customer.save(flush:true)
def account2 = Account.findById(account.id)println account2.customer
def customer2 = Customer.findById(customer.id)println customer2.accounts
Tuesday, March 30, 2010
![Page 30: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/30.jpg)
PostgreSQL East - March 25, 2010
Physical DB Settings - mapping
• Changing the table, versioning on/off, column mappings, indexing, multi-column-index, etc...
class Person {String firstNameString address static mapping = { table 'people' version false id column:'person_id' firstName column:'First_Name', index:'Name_Idx' address column:'Address', index:'Name_Idx, Address_Index' }}
Tuesday, March 30, 2010
![Page 31: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/31.jpg)
PostgreSQL East - March 25, 2010
Stored Procedures
• Several approaches
‣ Spring's JdbcTemplate
‣ Spring's StoredProcedure class
‣ Spring's SqlFunction
‣ Groovy SQL
• Demonstration
Tuesday, March 30, 2010
![Page 32: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/32.jpg)
PostgreSQL East - March 25, 2010
Where to find the code
• Hit our GitHub repository (you can just ask to download the files)
• GitHub repo for our samples
‣ github.com/krimple/Grails-Demos-PostgreSQL-East-2010.git
Tuesday, March 30, 2010
![Page 33: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/33.jpg)
PostgreSQL East - March 25, 2010
• Grails Web Site
‣ http://www.grails.org
• Grails User Email List
‣ http://n4.nabble.com/Grails-f1312388.html
• Groovy Web Site
‣ http://groovy.codehaus.org/
Groovy and Grails Resources
Tuesday, March 30, 2010
![Page 34: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/34.jpg)
PostgreSQL East - March 25, 2010
Free InfoQ E-Book
• Getting Started with Grails, Second Edition
‣ Scott Davis
‣ Jason Rudolph
• http://www.infoq.com/minibooks/grails-getting-started
Tuesday, March 30, 2010
![Page 35: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/35.jpg)
PostgreSQL East - March 25, 2010
Regional User Groups
• Philadelphia Groovy and Grails User Group
‣ http://phillygroovy.org
‣ Meet-up April 8 as part of Philly ETE
• Philadelphia Spring User Group
‣ http://phillyspring.ning.com/
Tuesday, March 30, 2010
![Page 36: Building Grails Applications with PostgreSQL · PostgreSQL East - March 25, 2010 About Brent and Ken • Brent Baxter: bbaxter@chariotsolutions.com ‣ Consultant and Applications](https://reader034.vdocuments.site/reader034/viewer/2022042309/5ed5e02a1518b64a38212998/html5/thumbnails/36.jpg)
PostgreSQL East - March 25, 2010
Thank you!
Questions ... ?
Tuesday, March 30, 2010