Java Reference
In-Depth Information
static
hasMany
= [knights:Knight, tasks:Task]
static
constraints
= {
name blank:
false
}
}
The
Quest
has a
name
property and an override of
toString
to return it. The keyword
hasMany
is part of GORM, the Grails Object-Relational Mapping DSL, which program-
matically configures Hibernate. Other ORM tools are available, but Hibernate is the de-
fault.The
hasMany
keywordimpliesaforeignkeyrelationshipbetweenboththe
Knight
and
Task
tables and the
Quest
table.
Domain classes also have constraints, which are enforced by Grails when creating new in-
stances. For the
Quest
, the
name
field cannot be empty.
The
Task
class is shown in the next listing.
Task
s have a
name
, a
priority
, a start
and end date, and a completion marker.
Listing 10.13.
Task
s belong to a
Quest
class Task {
String name
int priority = 3
Date startDate = new Date()
Date endDate = new Date()
boolean completed
String toString() { name }
static belongsTo = [quest:Quest]
static constraints = {
name blank:false
priority range:1..5
startDate()
endDate validator: { value, task ->
value >= task.startDate
}
completed()
}
}
The constraints closure states that
Task
s must have a name, a priority that falls between
1 and 5, and an end date that's greater than or equal to the start date. The other notable