Java Reference
In-Depth Information
Figure 8.3. An entity relationship diagram for the generated database, given the domain classes listed in the text
In this case the database is MySQL version 5, so the data type for
id
is
BIGINT
. It also
converts the camel case properties
dateCreated
and
lastUpdated
into underscores
in the table. Because the relationship between
Order
and
OrderLine
was unidirection-
al, Hibernate generates a join table between them called
orders_order_line
.
Grails also adds a column called
version
to each table. Hibernate uses this for optimistic
locking. That means whenever a row of a table is modified and saved, Hibernate will auto-
matically increment the version column by one. That's an attempt to get locking behavior
without actually locking a row and paying the resulting performance penalty. If the applic-
ation involves many reads but only a few writes, this works well. If there are too many
writes, Grails also adds an instance method called
lock
to each domain class that locks
the row. That's called
pessimistic locking
and will result in worse performance, so it's only
used when necessary.
Grails does far more than this. For example, Grails uses Groovy to generate dynamic finder
methods for each domain class. For the
Product
class, Grails generates static methods on
the domain class that include