Java Reference
In-Depth Information
Listing 8.14. Initialization code in BootStrap.groovy
class BootStrap {
def init = { servletContext ->
if (!Product.findByName('baseball')) {
Product baseball =
new Product(name:'baseball', price:5.99).save()
Product football =
new Product(name:'football', price:12.99).save()
Customer cb = new Customer(name:'Charlie Brown').save()
Order o1 = new Order(number:'1', customer:cb)
.addToOrderLines(product:baseball, quantity:2)
.addToOrderLines(product:football, quantity:1)
.save()
}
}
def destroy = {
}
}
The code in the init closure is executed when the application is started. The ad-
dToOrderLines method comes from declaring that an Order has many OrderLine
instances. The save method first validates each object against its constraints and then
saves it to the database.
Grails uses Hibernate's ability to generate a database schema. An entity relationship dia-
gram (ERD) for the generated database is shown in figure 8.3 . [ 12 ]
12 This diagram was generated using MySQL Workbench, which is a free tool available at www.mysql.com/
products/workbench/ .
 
Search WWH ::




Custom Search