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.

Grails domain classes

359 views

Published on

Grails domain introduction

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Grails domain classes

  1. 1. Grails - Domain Classes Hiten Pratap Singh hiten@nexthoughts.com https://github.com/hitenpratap
  2. 2. Domain Classes • Object oriented (OO) applications involve a domain model representing the business entities that the application deals with. • Domain classes has properties associated with them which map to a database in order to persist instances of those classes. • Domain class create : grails create-domain-class <<class_name>>
  3. 3. Persisting Fields to the Database • All the fields in a domain class are persisted to the database. • Each field in the class will map to a column in the database.
  4. 4. Validations • Grails allows you to apply constraints to a domain class that can then be used to validate a domain class instance. • Constraints are applied using a "constraints" closure. • To validate a domain class you can call the "validate()" method on an instance.
  5. 5. Example class User { String login String password String email Date age static constraints={ login(size:5..15,blank:false,unique:true) password(size:5..15,blank:false) email(email:true,blank:false) age(min:new Date(),nullable:false) }}
  6. 6. Validating Constraints def user = new User(params) if(user.validate()){ //do something }else{ user.errors.allErrors.each{ Println it } }
  7. 7. CustomValidators class User { static constraints = { password(unique:true, length:5..15, validator{val, obj > if(val?.equalsIgnoreCase(obj.firstName)){ return false } })}}
  8. 8. Transient Properties • Transient properties are never written to the database. • Every property in a domain class is persistent and required. • They don't have a corresponding column in the database.
  9. 9. Example • class Company { BigDecimal cash BigDecimal receivables BigDecimal getNetWorth() { cash + receivables } static transients = ['netWorth'] }
  10. 10. Custom Mapping class Person { String firstName String lastName Integer age static mapping = { id column:'person_id' firstName column:'person_first_name' lastName column:'person_last_name' age column:'person_age' version false } }
  11. 11. Relationships • Every table should have at least one relationship to another table. • The types of relationships that Grails supports are: • One-to-one • One-to-many • Many-to-one • Many-to-many
  12. 12. One-to-One Relationship class Car { Engine engine } class Engine { static belongsTo = [car:Car] }
  13. 13. One-to-many relationships class Artist { String name static hasMany = [albums:Album] }
  14. 14. Many-to-many relationships class Book { static belongsTo = Author static hasMany = [authors:Author] } class Author { static hasMany = [books:Book] }
  15. 15. Questions?
  16. 16. Hiten Pratap Singh hiten@nexthoughts.com https://github.com/hitenpratap

×