Download - Grails domain classes
![Page 2: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/2.jpg)
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>>
![Page 3: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/3.jpg)
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.
![Page 4: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/4.jpg)
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.
![Page 5: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/5.jpg)
Exampleclass 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)
}}
![Page 6: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/6.jpg)
Validating Constraints
def user = new User(params) if(user.validate()){ //do something }else{ user.errors.allErrors.each{ Println it } }
![Page 7: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/7.jpg)
Custom Validators
class User { static constraints = { password(unique:true, length:5..15, validator{val, obj > if(val?.equalsIgnoreCase(obj.firstName)){ return false } })}}
![Page 8: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/8.jpg)
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.
![Page 9: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/9.jpg)
Example
• class Company { BigDecimal cash BigDecimal receivables BigDecimal getNetWorth() { cash + receivables } static transients = ['netWorth'] }
![Page 10: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/10.jpg)
Custom Mappingclass 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 }}
![Page 11: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/11.jpg)
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
![Page 12: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/12.jpg)
One-to-One Relationship
class Car { Engine engine}
class Engine { static belongsTo = [car:Car]}
![Page 13: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/13.jpg)
One-to-many relationships
class Artist {
String name
static hasMany = [albums:Album]
}
![Page 14: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/14.jpg)
Many-to-many relationships
class Book { static belongsTo = Author static hasMany = [authors:Author]}
class Author { static hasMany = [books:Book]}
![Page 15: Grails domain classes](https://reader036.vdocuments.us/reader036/viewer/2022083105/587def111a28abab7e8b487b/html5/thumbnails/15.jpg)
Questions?